An implementation of the Maybe monad in Typescript.
To install, run:
yarn add @jdeurt/maybe
Maybe<A>
is an abstract class that represents a type that may either hold a value of type A
(represented as Just<A>
) or not (represented as Nothing<A>
).
Creates a new Maybe<A>
instance.
Creates a new Nothing<A>
instance.
Creates a new Just<A>
instance.
Returns true
if the Maybe<A>
instance is Nothing<A>
, false
otherwise.
Returns true
if the Maybe<A>
instance is Just<A>
, false
otherwise.
Transforms the value in the Just<A>
using the function, if the Maybe<A>
is Just<A>
. If the Maybe<A>
is Nothing<A>
, does nothing.
Similar to map
, but the provided function should return a Maybe<B>
instance.
Applies the function contained in the Maybe<A>
to another Maybe<A>
, if both Maybe<A>
instances are Just<A>
. If either Maybe<A>
is Nothing<A>
, does nothing.
Checks if two Maybe<A>
instances are equal.
If the Maybe<A>
instance is Just<A>
, returns its value, otherwise returns the default value.
If the Maybe<A>
instance is Just<A>
, returns it, otherwise returns Just<A>
of the default value.
Returns the value of the Maybe<A>
instance if it's Just<A>
, otherwise throws an error.
Returns the string representation of the Maybe<A>
instance.
Nothing<A>
is a class that represents the absence of a value. It extends Maybe<A>
.
Creates a new Nothing<A>
instance.
Just<A>
is a class that represents the presence of a value. It extends Maybe<A>
.
Creates a new Just<A>
instance.