either.md

December 15, 2024 ยท View on GitHub

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.of(v)

Creates a Right Either.

ParamTypeDescription
vanyValue

Either.Right(v)

Creates a Right Either.

ParamTypeDescription
vanyValue

Either.Left(v)

Creates a Left Either.

ParamTypeDescription
vanyValue

Either.fromNullable(v)

Creates a Right if the value is not null or undefined; otherwise, creates a Left.

ParamTypeDescription
vanyValue

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.

ParamTypeDescription
defanyDefault value
vanyValue

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.

ParamTypeDescription
ffunctionA 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.

ParamTypeDescription
eanyEither type
flfunctionFunction to be applied on the Left element
frfunctionFunction to be applied on the Right element

Either.isLeft(e)

A static method that returns true if the passed Either is a Left.

ParamTypeDescription
eanyEither type

Either.isRight(e)

A static method that returns true if the passed Either is a Right.

ParamTypeDescription
eanyEither type

Either.equals(n)

Returns true if the current and the passed element are of Either type with the same value.

ParamTypeDescription
nanyAny value of type Setoid

Either.map(f)

Applies the passed function to the value of the current Either if it is a Right.

ParamTypeDescription
ffunctionFunction

Either.bimap(fl, fr)

Applies fl to the Left element or fr to the Right element of the current Either.

ParamTypeDescription
flfunctionFunction to be applied on the Left element
frfunctionFunction to be applied on the Right element

Either.chain(f)

An instance method that can chain together many computations that return an Either type.

ParamTypeDescription
ffunctionFunction 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.

ParamTypeDescription
janyEither with a function

Either.getValue()

Gets the value inside the Either.