Open fvilante opened 4 years ago
Good: fold
and bimap
are preatty intercallable
const a0 = f1.fold(
err => `Finished: Throw this error --> ${err}`,
value => `Finished: Successful, value --> ${ value }`,
)
const a1 = f1.bimap(
err => `Finished: Throw this error --> ${err}`,
value => `Finished: Successful, value --> ${ value }`,
)
Difference folde
and match
:
fold: <R>(errorFn: (_: E) => R, valueFn: (_:A) => R) => Result<void,R>
match: <R>(errorFn: (_: E) => R, valueFn: (_:A) => R) => R
Important to note that this structure is Recursive Monad<E,Monad<E,A>> and the normaly in a
flatten` operation the first E may abort the perception of the second.
State clearly in docs: DO NOT USE TYPE COERSION, unless you know exactly what you are doing. Type coersion is rarelly necessary. If you do it wrong you compromise type safety of all code downstream. Show cases of Either
where some type annotation
(not coersion!) is necessary.
Docs should alert that is not expected to use functions like the update (bellow) to cause side-effect. They can monitor the number that are flowing but avoid to cause side-effect
function f
also must be pure.
/** Models safe natural numbers. ex: 1, 2, 3...*/
type Natural = {
readonly kind: 'Natural'
readonly run: () => Result<string,number>
readonly update: (f: (n: number) => number) => Natural
}
unsafeRun and Run convention on Algebraic types
unsafe is used in when you break the 'lazyness' contract, if you do not break the contract applying that combinator the convention is to use just run without 'unsafe' term.
Any promise inside a lazy-pure type like in
Result<E,Promise<A>>
is impure.Instead of Promise use Future everywhere.