data.maybe
¶Stability: | 3 - Stable |
---|---|
Bug Tracker: | https://github.com/folktale/data.maybe/issues |
Version: | 1.2.0 |
Repository: | https://github.com/folktale/data.maybe |
Portability: | Portable |
npm package: | data.maybe |
A structure for values that may not be present, or computations that may fail.
The class models two different cases:
Just α
— represents aMaybe(α)
that contains a value.α
may be any value, includingnull
andundefined
.Nothing
— represents aMaybe(α)
that has no values. Or a failure that needs no additional information.
Require the data.maybe
package, after installing it:
var Maybe = require('data.maybe')
This gives you back a data.maybe.Maybe
object.
The Maybe(α)
structure explicitly models the effects that are implicit
in Nullable
types, thus has none of the problems associated with using
null
or undefined
, such as NullPointerException
or TypeError:
undefined is not a function
.
Common uses of this structure includes modelling values that may or may not
be present. For example, instead of having both a collection.has(a)
and
collection.get(a)
operation, one may have the collection.get(a)
operation return a Maybe(α)
value. This avoids a problem of data
incoherence (specially in asynchronous collections, where a value may be
added between a call to .has()
and .get()
!).
Another common usage is for modelling functions that might fail to provide a
value. E.g.: collection.find(predicate)
can safely return a Maybe(α)
instance, even if the collection allows nullable values.