The class models two different cases:
Just a
— represents a Maybe(a)
that contains a value. a
may be
any value, including null
or undefined
.Nothing
— represents a Maybe(a)
that has no values. Or a
failure that needs no additional information.Common uses of this monad includes modelling values that may or may
not be present in a collection, thus instead of needing a
collection.has(a)
, the collection.get(a)
operation gives you all
the information you need — collection.get(a).is-nothing
being
equivalent to collection.has(a)
; Similarly the same reasoning may be
applied to computations that may fail to provide a value, e.g.:
collection.find(predicate)
can safely return a Maybe(a)
instance,
even if the collection contains nullable values.
Furthermore, the values of Maybe(a)
can be combined and manipulated
by using the expressive monadic operations. This allows safely
sequencing operations that may fail, and safely composing values that
you don't know whether they're present or not, failing early
(returning a Nothing
) if any of the operations fail.
If one wants to store additional information about failures, the
Either and Validation monads provide such a capability, and
should be used instead of the Maybe(a)
monad.
The Maybe(a)
monad.
Constructs a new Maybe(a)
monad with an absent value — i.e.:
represents a failure.
Constructs a new Maybe(a)
monad that holds the single value a
.
a
can be any value, including null
, undefined
or another
Maybe(a)
monad.
Constructs a new Maybe(a)
monad from a nullable type. If the value
is either null
or undefined
, this function returns a Nothing
,
otherwise the value is wrapped in a Just(a)
.
True if the Maybe(a)
monad contains a failure (i.e.: Nothing
).
True if the Maybe(a)
monad contains a single value (i.e.:
Just(a)
).
Creates a new Maybe(a)
monad holding the single value a
.
a
can be any value, includding null
, undefined
or another
Maybe(a)
monad.
Applies the function inside the Maybe(a)
monad to another
applicative type.
The Maybe(a)
monad should contain a function value, otherwise a
TypeError
is thrown.
Transforms the value of the Maybe(a)
monad using a regular unary
function.
Transforms the value of the Maybe(a)
monad using an unary function
to a monad of the same type.
Returns a textual representation of the Maybe(a)
monad.
Tests if a Maybe(a)
monad is equal to another Maybe(a)
monad.
Extracts the value out of the Maybe(a)
monad, if it
exists. Otherwise throws a TypeError
.
Nothing
).Extracts the value out of the Maybe(a)
monad. If there is no
value, returns the given default.
Transforms a failure into a new Maybe(a)
monad. Does nothing if
the monad already contains a value.
Represents a Maybe(a)
holding a value.
A singleton representing a Maybe(a)
holding no value.
Monad: Maybe(a)
A Monad for values that may not be present, or computations that may fail.
Maybe(a)
explicitly models the effects that are implicit inNullable
types, thus has none of the problems associated withnull
orundefined
— likeNullPointerExceptions
.