MODULE Either
data
type Either<L, A> = Left<L, A> | Right<L, A>
Represents a value of one of two possible types (a disjoint union).
An instance of Either
is either an instance of Left
or Right
.
A common use of Either
is as an alternative to Option
for dealing with possible missing values. In this usage,
None
is replaced with a Left
which can contain useful information. Right
takes the place of Some
. Convention
dictates that Left
is used for failure and Right
is used for success.
For example, you could use Either<string, number>
to detect whether a received input is a string
or a number
.
const parse = (errorMessage: string) => (input: string): Either<string, number> => {
const n = parseInt(input, 10)
return isNaN(n) ? left(errorMessage) : right(n)
}
Either
is right-biased, which means that Right
is assumed to be the default case to operate on. If it is Left
,
operations like map
, chain
, ... return the Left
value unchanged:
right(12).map(double) // right(24)
left(23).map(double) // left(23)
(fy: Either<L, A>): Either<L, A>
<B>(fab: Either<L, (a: A) => B>): Either<L, B>
<B, C>(this: Either<L, (b: B) => C>, fb: Either<L, B>): Either<L, C>
<V, B>(f: (l: L) => V, g: (a: A) => B): Either<V, B>
<B>(f: (a: A) => Either<L, B>): Either<L, B>
Binds the given function across Right
<B>(f: (ea: Either<L, A>) => B): Either<L, B>
<B>(left: (l: L) => B, right: (a: A) => B): B
Applies a function to each case in the data structure
(a: A): A
Returns the value from this Right
or the given argument if this is a Left
(f: (l: L) => A): A
Returns the value from this Right
or the result of given argument if this is a Left
(): string
(): this is Left<L, A>
Returns true
if the either is an instance of Left
, false
otherwise
(): this is Right<L, A>
Returns true
if the either is an instance of Right
, false
otherwise
<B>(f: (a: A) => B): Either<L, B>
The given function is applied if this is a Right
<M>(f: (l: L) => M): Either<M, A>
Maps the left side of the disjunction
<B>(b: B, f: (b: B, a: A) => B): B
(): Either<A, L>
Swaps the disjunction values
(): string
instance
Monad2<URI> &
Foldable2<URI> &
Traversable2<URI> &
Bifunctor2<URI> &
Alt2<URI> &
Extend2<URI> &
ChainRec2<URI>
function
<L>(defaultValue: L) => <A>(a: A | null | undefined): Either<L, A>
Takes a default and a nullable value, if the value is not nully, turn it into a Right
, if the value is nully use the
provided default as a Left
function
<L>(defaultValue: L) => <A>(fa: Option<A>): Either<L, A>
Takes a default and a Option
value, if the value is a Some
, turn it into a Right
, if the value is a None
use the
provided default as a Left
function
<L, A>(predicate: Predicate<A>, whenFalse: (a: A) => L) => (a: A): Either<L, A>
function
<L, A>(fa: Validation<L, A>): Either<L, A>
function
<L, A>(SL: Setoid<L>, SA: Setoid<A>): Setoid<Either<L, A>>
function
<L, A>(fa: Either<L, A>): fa is Left<L, A>
Returns true
if the either is an instance of Left
, false
otherwise
function
<L, A>(fa: Either<L, A>): fa is Right<L, A>
Returns true
if the either is an instance of Right
, false
otherwise
function
<L, A>(l: L): Either<L, A>
Constructs a new Either
holding a Left
value. This usually represents a failure, due to the right-bias of this
structure
function Alias of
of
Constructs a new Either
holding a Right
value. This usually represents a successful value due to the right bias of
this structure
function
(e: {}): Error
Default value for the optional onerror
argument of tryCatch
function
<A>(f: Lazy<A>, onerror: (e: {}) => Error = toError): Either<Error, A>