Overview

A data structure providing “inclusive-or” as opposed to Either’s “exclusive-or”.

If you interpret Either<E, A> as suggesting the computation may either fail or succeed (exclusively), then These<E, A> may fail, succeed, or do both at the same time.

There are a few ways to interpret the both case:

  • You can think of a computation that has a non-fatal error.
  • You can think of a computation that went as far as it could before erroring.
  • You can think of a computation that keeps track of errors as it completes.

Another way you can think of These<E, A> is saying that we want to handle E kind of data, A kind of data, or both E and A kind of data at the same time. This is particularly useful when it comes to displaying UI’s.

(description adapted from https://package.elm-lang.org/packages/joneshf/elm-these)

Adapted from https://github.com/purescript-contrib/purescript-these


Table of contents


Both (interface)

Signature

export interface Both<E, A> {
  readonly _tag: 'Both'
  readonly left: E
  readonly right: A
}

Added in v2.0.0

These (type alias)

Signature

export type These<E, A> = Either<E, A> | Both<E, A>

Added in v2.0.0

URI (type alias)

Signature

export type URI = typeof URI

Added in v2.0.0

URI (constant)

Signature

export const URI: "These" = ...

Added in v2.0.0

these (constant)

Signature

export const these: Functor2<URI> & Bifunctor2<URI> & Foldable2<URI> & Traversable2<URI> = ...

Added in v2.0.0

both (function)

Signature

export function both<E, A>(left: E, right: A): These<E, A> { ... }

Added in v2.0.0

fold (function)

Signature

export function fold<E, A, B>(
  onLeft: (e: E) => B,
  onRight: (a: A) => B,
  onBoth: (e: E, a: A) => B
): (fa: These<E, A>) => B { ... }

Added in v2.0.0

fromOptions (function)

Takes a pair of Options and attempts to create a These from them

Signature

export function fromOptions<E, A>(fe: Option<E>, fa: Option<A>): Option<These<E, A>> { ... }

Example

import { fromOptions, left, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(fromOptions(none, none), none)
assert.deepStrictEqual(fromOptions(some('a'), none), some(left('a')))
assert.deepStrictEqual(fromOptions(none, some(1)), some(right(1)))
assert.deepStrictEqual(fromOptions(some('a'), some(1)), some(both('a', 1)))

Added in v2.0.0

getEq (function)

Signature

export function getEq<E, A>(EE: Eq<E>, EA: Eq<A>): Eq<These<E, A>> { ... }

Added in v2.0.0

getLeft (function)

Returns an L value if possible

Signature

export function getLeft<E, A>(fa: These<E, A>): Option<E> { ... }

Example

import { getLeft, left, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(getLeft(left('a')), some('a'))
assert.deepStrictEqual(getLeft(right(1)), none)
assert.deepStrictEqual(getLeft(both('a', 1)), some('a'))

Added in v2.0.0

getLeftOnly (function)

Returns the L value if and only if the value is constructed with Left

Signature

export function getLeftOnly<E, A>(fa: These<E, A>): Option<E> { ... }

Example

import { getLeftOnly, left, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(getLeftOnly(left('a')), some('a'))
assert.deepStrictEqual(getLeftOnly(right(1)), none)
assert.deepStrictEqual(getLeftOnly(both('a', 1)), none)

Added in v2.0.0

getMonad (function)

Signature

export function getMonad<E>(S: Semigroup<E>): Monad2C<URI, E> { ... }

Added in v2.0.0

getRight (function)

Returns an A value if possible

Signature

export function getRight<E, A>(fa: These<E, A>): Option<A> { ... }

Example

import { getRight, left, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(getRight(left('a')), none)
assert.deepStrictEqual(getRight(right(1)), some(1))
assert.deepStrictEqual(getRight(both('a', 1)), some(1))

Added in v2.0.0

getRightOnly (function)

Returns the A value if and only if the value is constructed with Right

Signature

export function getRightOnly<E, A>(fa: These<E, A>): Option<A> { ... }

Example

import { getRightOnly, left, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(getRightOnly(left('a')), none)
assert.deepStrictEqual(getRightOnly(right(1)), some(1))
assert.deepStrictEqual(getRightOnly(both('a', 1)), none)

Added in v2.0.0

getSemigroup (function)

Signature

export function getSemigroup<E, A>(SL: Semigroup<E>, SA: Semigroup<A>): Semigroup<These<E, A>> { ... }

Added in v2.0.0

getShow (function)

Signature

export function getShow<E, A>(SE: Show<E>, SA: Show<A>): Show<These<E, A>> { ... }

Added in v2.0.0

isBoth (function)

Returns true if the these is an instance of Both, false otherwise

Signature

export function isBoth<E, A>(fa: These<E, A>): fa is Both<E, A> { ... }

Added in v2.0.0

isLeft (function)

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

Signature

export function isLeft<E, A>(fa: These<E, A>): fa is Left<E> { ... }

Added in v2.0.0

isRight (function)

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

Signature

export function isRight<E, A>(fa: These<E, A>): fa is Right<A> { ... }

Added in v2.0.0

left (function)

Signature

export function left<E = never, A = never>(left: E): These<E, A> { ... }

Added in v2.0.0

leftOrBoth (function)

Signature

export function leftOrBoth<E>(defaultLeft: E): <A>(ma: Option<A>) => These<E, A> { ... }

Example

import { leftOrBoth, left, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(leftOrBoth('a')(none), left('a'))
assert.deepStrictEqual(leftOrBoth('a')(some(1)), both('a', 1))

Added in v2.0.0

right (function)

Signature

export function right<E = never, A = never>(right: A): These<E, A> { ... }

Added in v2.0.0

rightOrBoth (function)

Signature

export function rightOrBoth<A>(defaultRight: A): <E>(me: Option<E>) => These<E, A> { ... }

Example

import { rightOrBoth, right, both } from 'fp-ts/lib/These'
import { none, some } from 'fp-ts/lib/Option'

assert.deepStrictEqual(rightOrBoth(1)(none), right(1))
assert.deepStrictEqual(rightOrBoth(1)(some('a')), both('a', 1))

Added in v2.0.0

toTuple (function)

Signature

export function toTuple<E, A>(e: E, a: A): (fa: These<E, A>) => [E, A] { ... }

Example

import { toTuple, left, right, both } from 'fp-ts/lib/These'

assert.deepStrictEqual(toTuple('a', 1)(left('b')), ['b', 1])
assert.deepStrictEqual(toTuple('a', 1)(right(2)), ['a', 2])
assert.deepStrictEqual(toTuple('a', 1)(both('b', 2)), ['b', 2])

Added in v2.0.0

bimap (export)

Signature

<E, G, A, B>(f: (e: E) => G, g: (a: A) => B) => (fa: These<E, A>) => These<G, B>

Added in v2.0.0

foldMap (export)

Signature

;<M>(M: Monoid<M>) => <A>(f: (a: A) => M) => <E>(fa: These<E, A>) => M

Added in v2.0.0

map (export)

Signature

<A, B>(f: (a: A) => B) => <E>(fa: These<E, A>) => These<E, B>

Added in v2.0.0

mapLeft (export)

Signature

<E, G>(f: (e: E) => G) => <A>(fa: These<E, A>) => These<G, A>

Added in v2.0.0

reduce (export)

Signature

;<A, B>(b: B, f: (b: B, a: A) => B) => <E>(fa: These<E, A>) => B

Added in v2.0.0

reduceRight (export)

Signature

;<A, B>(b: B, f: (a: A, b: B) => B) => <E>(fa: These<E, A>) => B

Added in v2.0.0