This class models two different cases: Left a
and Right b
, and can
hold one of the cases at any given time. The projections are, none the
less, biased for the Right
case, thus a common use case for this
monad is to hold the results of computations that may fail, when you
want to store additional information on the failure (instead of
throwing an exception).
Furthermore, the values of Either(a, b)
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 Left a
) if any of the operations fail.
While this class can certainly model input validations, the Validation monad lends itself better to that use case, since it can naturally aggregate failures — monads shortcut on the first failure.
The Either(a, b)
monad.
Constructs a new Either(a, b)
monad holding a Left
value. This
usually represents a failure due to the right-bias of this monad.
Constructs a new Either(a, b)
monad holding a Right
value. This
usually represents a successful value due to the right-bias of this
monad.
Constructs a new Either(a, b)
from a nullable type. Takes the
Left
case if the value is null
or undefined
. Takes the Right
case otherwise.
+ type: a -> Either(a, a)
True if the Either(a, b)
contains a Left
value.
+ type: Boolean
True if the Either(a, b)
contains a Right
value.
+ type: Boolean
Creates a new Either(a, b)
instance holding the Right
value
b
.
b
can be any value, including null
, undefined
or another
Either(a, b)
monad.
Applies the function inside the Right
case of the Either(a, b)
monad to another applicative type.
The Either(a, b)
monad should contain a function value, otherwise
a TypeError
is thrown.
+ type: (@Either(a, b -> c), f:Applicative) => f(b) -> f(c)
Transforms the Right
value of the Either(a, b)
monad using a
regular unary function.
Transforms the Right
value of the Either(a, b)
monad using an
unary function to a monad of the same type.
Returns a textual representation of the Either(a, c)
monad.
Tests if an Either(a, b)
monad is equal to another Either(a, b)
monad.
+ type: (@Either(a, b)) => Either(a, b) -> Boolean
Extracts the Right
value out of the Either(a, b)
monad, if it
exists. Otherwise throws a TypeError
.
Right
value.Extracts the Right
value out of the Either(a, b)
monad. If
the monad doesn't have a Right
value, returns the given default.
+ type: (@Either(a, b)) => b -> b
Transforms a Left
value into a new Either(a, b)
monad. Does
nothing if the monad contains a Right
value.
+ type: (@Either(a, b)) => (a -> Either(c, b)) -> Either(c, b)
Returns the value of whichever side of the disjunction that is present. + type: (@Either(a, a)) => Unit -> a
Catamorphism. Takes two functions, applies the leftmost one to the
Left
value and the rightmost one to the Right
value, depending
on which one is present.
+ type: (@Either(a, b)) => (a -> c) -> (b -> c) -> c
Swaps the disjunction values.
Maps both sides of the disjunction. + type: (@Either(a, b)) => (a -> c) -> (b -> d) -> Either(c, d)
Maps the left side of the disjunction.
Represents the Right
side of the disjunction.
Represents the Left
side of the disjunction.
Monad: Either(a, b)
The
Either(a, b)
monad represents the logical disjunction betweena
andb
. In other words,Either
may contain either a value of typea
or a value of typeb
, at any given time. This particular implementation is biased on the right value (b
), thus projections will take the right value over the left one.