Either
Either
Either
is a monad and can be used as a generic structure for a type with two possibilities: a Left a or a Right b. It represents the logical disjunction between a
and b
.
A common use of this structure is to handle error cases and situations where a computation might fail, while also providing additional information about the failure. It is used to represent a value that is either correct or an error. By convention, the Left constructor is used to hold an error value, and the Right constructor is used to hold a correct value. This structure forces explicit handling of failures and avoids the problems associated with throwing exceptions.
Implements: BiFunctor
, Monad
, Setoid
- Either
- Either.of(v)
- Either.Right(v)
- Either.Left(v)
- Either.fromNullable(v)
- Either.withDefault(def, v)
- Either.swap()
- Either.try(f)
- Either.bimap(e, fl, fr)
- Either.isLeft(e)
- Either.isRight(e)
- Either.equals(n)
- Either.map(f)
- Either.bimap(fl, fr)
- Either.chain(f)
- Either.swap()
- Either.isLeft()
- Either.isRight()
- Either.ap(j)
- Either.getValue()
Either.of(v)
Creates a Right Either
.
Param | Type | Description |
---|---|---|
v | any |
Value |
Either.Right(v)
Creates a Right Either
.
Param | Type | Description |
---|---|---|
v | any |
Value |
Either.Left(v)
Creates a Left Either
.
Param | Type | Description |
---|---|---|
v | any |
Value |
Either.fromNullable(v)
Creates a Right if the value is not null
or undefined
; otherwise, creates a Left.
Param | Type | Description |
---|---|---|
v | any |
Value |
Either.withDefault(def, v)
Creates a Right if the value v
is not null
or undefined
; otherwise, creates a Right with the default value def
.
Param | Type | Description |
---|---|---|
def | any |
Default value |
v | any |
Value |
Either.swap()
Swaps the Left and Right elements of the current Either
.
Either.try(f)
Executes the passed function that may throw and converts it to an Either
type.
Param | Type | Description |
---|---|---|
f | function |
A function that may throw an error |
Either.bimap(e, fl, fr)
A static method that applies fl
to the Left element or fr
to the Right element of the current Either
.
Param | Type | Description |
---|---|---|
e | any |
Either type |
fl | function |
Function to be applied on the Left element |
fr | function |
Function to be applied on the Right element |
Either.isLeft(e)
A static method that returns true
if the passed Either
is a Left.
Param | Type | Description |
---|---|---|
e | any |
Either type |
Either.isRight(e)
A static method that returns true
if the passed Either
is a Right.
Param | Type | Description |
---|---|---|
e | any |
Either type |
Either.equals(n)
Returns true
if the current and the passed element are of Either
type with the same value.
Param | Type | Description |
---|---|---|
n | any |
Any value of type Setoid |
Either.map(f)
Applies the passed function to the value of the current Either
if it is a Right.
Param | Type | Description |
---|---|---|
f | function |
Function |
Either.bimap(fl, fr)
Applies fl
to the Left element or fr
to the Right element of the current Either
.
Param | Type | Description |
---|---|---|
fl | function |
Function to be applied on the Left element |
fr | function |
Function to be applied on the Right element |
Either.chain(f)
An instance method that can chain together many computations that return an Either
type.
Param | Type | Description |
---|---|---|
f | function |
Function that returns another Either |
Either.swap()
Swaps the Left and Right elements of the current Either
.
Either.isLeft()
An instance method that returns true
if the current Either
is a Left.
Either.isRight()
An instance method that returns true
if the current Either
is a Right.
Either.ap(j)
Applies the function inside the passed Either
to the current Either
if it is a Right.
Param | Type | Description |
---|---|---|
j | any |
Either with a function |
Either.getValue()
Gets the value inside the Either
.