Encapsulates a common pattern of needing to build up an Object from
a series of Maybe values. This is often solved by nesting andThen
calls
and then completing the chain with a call to success
.
This feature was inspired (and the code lifted from) this article: https://medium.com/@dhruvrajvanshi/simulating-haskells-do-notation-in-typescript-e48a9501751c
Wrapped values are converted to an Object using the Object constructor before assigning. Primitives won't fail at runtime, but results may be unexpected.
Folds over types; a switch/case for Just/Nothing.
Inject a side-effectual operation into a chain of maybe operations.
The primary use case for do
is to perform logging in the middle of a flow
of Maybe computations.
The side effect only runs when there is a value (Just).
The value will (should) remain unchanged during the do
operation.
just({}) .assign('foo', just(42)) .assign('bar', just('hello')) .do(scope => console.log('Scope: ', JSON.stringify(scope))) .map(doSomethingElse)
Returns the maybe value if it is nonempty. Otherwise returns the result of evaluating fn.
Returns the maybe value if it is nonempty. Otherwise returns the defaultValue.
If there's a value, apply the function and return a new Maybe. Mapping over Nothing returns Nothing.
Generated using TypeDoc
Maybe describes a value that is optional. It is safer to use then null and undefined.