Tuple

SetoidFunctorBifunctorApply
Tuple, also known as Pair, is a data type containing two values. You can think of it as an immutable array of only two elements, but unlike arrays (which are commonly homogeneous), the two values inside can be of different types.
How to import
import { Tuple } from 'purify-ts/Tuple'

Constructors

Tuple
a -> b -> (a, b)<F, S>(fst: F, snd: S): Tuple<F, S>
Constructs a tuple.
Tuple(1, true)
Tuple(1, true) // Tuple<number, boolean>

Static methods

fromArray
<F, S>([fst, snd]: [F, S]): Tuple<F, S>
Constructs a tuple from an array with two elements.
Tuple.fromArray([5, 10])
Tuple(5, 10)
fanout
(a -> b) -> (a -> c) -> a -> (b, c)<F, S, T>(f: (value: T) => F, g: (value: T) => S, value: T): Tuple<F, S>
Applies two functions over a single value and constructs a tuple from the results.
Tuple.fanout(x => x[0], x => x.length, 'sss')
Tuple.fanout(x => x[0], x => x.length)('sss')
Tuple('s', 3)
Tuple('s', 3)

Instance methods

[Symbol.iterator]
`Tuple` implements the Iterator and ArrayLike interfaces, which means that you can destructure tuples like you would destructure arrays.
const [ fst, snd ] = Tuple(1, 'str')
// Types are preserved - fst has type of number, snd has type of string
fst
(a, b) ~> a(): F
Returns the first value of `this`.
Tuple(5, 10).fst()
5
snd
(a, b) ~> b(): S
Returns the second value of `this`.
Tuple(5, 10).snd()
10
equals
(a, b) ~> (a, b) -> Boolother: Tuple<F, S>): boolean
Compares the values inside `this` and another tuple.
Tuple(5, 10).equals(Tuple(5, 10))
Tuple(5, 'foo').equals(5, 'bar')
true
false
bimap
(a, b) ~> (a -> c) -> (b -> d) -> (c, d)<F2, S2>(f: (fst: F) => F2, g: (snd: S) => S2): Tuple<F2, S2>
Transforms the two values inside `this` with two mapper functions.
Tuple(1, 'apple').bimap(x => x + 1, x => x + 's')
Tuple(2, 'apples')
map
(a, b) ~> (b -> c) -> (a, c)<S2>(f: (snd: S) => S2): Tuple<F, S2>
Applies a function to the second value of `this`.
Tuple('configured', false).map(x => !x)
Tuple('configured', true)
mapFirst
(a, b) ~> (a -> c) -> (c, b)<F2>(f: (fst: F) => F2): Tuple<F2, S>
Applies a function to the first value of `this`.
Tuple(2, 'items').mapFirst(x => x + 1)
Tuple(3, 'items')
ap
(a, b) ~> (c, (b -> d)) -> (a, d)<T, S2>(f: Tuple<T, (value: S) => S2>): Tuple<F, S2>
Applies the second value of a tuple to the second value of `this`.
Tuple(5, 10).ap(Tuple('increment', x => x + 1))
Tuple(5, 11)
reduce
(a, b) ~> (b -> a -> b) -> b -> b<T>(reducer: (accumulator: T, value: S) => T, initialValue: T): T
A somewhat arbitrary implementation of Foldable for Tuple, the reducer will be passed the initial value and the second value inside `this` as arguments.
Tuple(5, 10).reduce((acc, x) => acc + x, 10)
20
swap
(a, b) ~> (b, a)(): Tuple<S, F>
Swaps the values inside `this`.
Tuple(0, 1).swap()
Tuple(1, 0)
toArray
(): [F, S]
Returns an array with 2 elements - the values inside `this`.
Tuple('username', true).toArray()
['username', true]
some
(a, a) ~> (a -> Bool) -> Bool(pred: (value: F | S) => boolean): boolean
Tests whether at least one element in the tuple passes the test implemented by the provided function.
every
(a, a) ~> (a -> Bool) -> Bool(pred: (value: F | S) => boolean): boolean
Tests whether both elements in the tuple pass the test implemented by the provided function.