Skip to content

Latest commit

 

History

History
276 lines (179 loc) · 4.38 KB

Either.md

File metadata and controls

276 lines (179 loc) · 4.38 KB

MODULE Either

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)

Methods

alt

(fy: Either<L, A>): Either<L, A>

ap

<B>(fab: Either<L, (a: A) => B>): Either<L, B>

ap_

<B, C>(this: Either<L, (b: B) => C>, fb: Either<L, B>): Either<L, C>

bimap

<V, B>(f: (l: L) => V, g: (a: A) => B): Either<V, B>

chain

<B>(f: (a: A) => Either<L, B>): Either<L, B>

Binds the given function across Right

extend

<B>(f: (ea: Either<L, A>) => B): Either<L, B>

fold

<B>(left: (l: L) => B, right: (a: A) => B): B

Applies a function to each case in the data structure

getOrElse

(a: A): A

Returns the value from this Right or the given argument if this is a Left

getOrElseL

(f: (l: L) => A): A

Returns the value from this Right or the result of given argument if this is a Left

inspect

(): string

isLeft

(): this is Left<L, A>

Returns true if the either is an instance of Left, false otherwise

isRight

(): this is Right<L, A>

Returns true if the either is an instance of Right, false otherwise

map

<B>(f: (a: A) => B): Either<L, B>

The given function is applied if this is a Right

mapLeft

<M>(f: (l: L) => M): Either<M, A>

Maps the left side of the disjunction

reduce

<B>(b: B, f: (b: B, a: A) => B): B

swap

(): Either<A, L>

Swaps the disjunction values

toString

(): string

either

instance

Monad2<URI> &
  Foldable2<URI> &
  Traversable2<URI> &
  Bifunctor2<URI> &
  Alt2<URI> &
  Extend2<URI> &
  ChainRec2<URI>

fromNullable

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

fromOption

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

fromPredicate

function

<L, A>(predicate: Predicate<A>, whenFalse: (a: A) => L) => (a: A): Either<L, A>

fromValidation

function

<L, A>(fa: Validation<L, A>): Either<L, A>

getSetoid

function

<L, A>(SL: Setoid<L>, SA: Setoid<A>): Setoid<Either<L, A>>

isLeft

function

<L, A>(fa: Either<L, A>): fa is Left<L, A>

Returns true if the either is an instance of Left, false otherwise

isRight

function

<L, A>(fa: Either<L, A>): fa is Right<L, A>

Returns true if the either is an instance of Right, false otherwise

left

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

right

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

toError

function

(e: {}): Error

Default value for the optional onerror argument of tryCatch

tryCatch

function

<A>(f: Lazy<A>, onerror: (e: {}) => Error = toError): Either<Error, A>