Skip to content

Latest commit

 

History

History
166 lines (107 loc) · 4.13 KB

pair.md

File metadata and controls

166 lines (107 loc) · 4.13 KB

Pair

A Pair is another way of storing two values in a single value. Elements of a Pair do not need to be of the same type

Implements: BiFunctor, Monad, Semigroup, Setoid

Pair(v1, v2)

Pair constructor

Param Type Description
v1 any First element of the Pair
v2 any second element of the Pair

Pair.of(v)

Pair constructor

Param Type Description
v any Element that will be stored as first and second element of the Pair

Pair.equals(j)

Check if the values of the current pair is equal to the values of the passed Pair. Pairs are equal only if the first and second values of both Pairs are equal

Param Type Description
j Pair The Pair to compare with

Pair.concat( p)

Concat the current pair with the passed one. Note that both first and second elements of the Pair should be of type semigroup for cancat to work

Param Type Description
p Pair Pair to concat

Pair.fst()

Get the first element of the Pair

Pair.snd()

Get the second element of the Pair

Pair.concat(j)

Applies the function inside the second element of the passed Pair to the current Pair and concats the first element of the second Pair to the first element of the current Pair

Param Type Description
j Pair Pair with function as the second element

Pair.getValue()

Get the values within the Pair as an Array of length 2

Pair.map(f)

Apply the function to the second element of the current Pair

Param Type Description
f function Function

Pair.bimap(f1, f2)

Apply f1 to the first element and f2 to the second element of the current Pair

Param Type Description
f1 function Function to be applied on the first element
f2 function Function to be applied on the second element

Pair.chain(f)

Chain together many computations that return a Pair

Param Type Description
f function Function that returns another Pair

Pair.swap()

Swap the elements of the current pair

Pair.toString()

Get a stringified version of the Pair


Examples

Lets construct some Pairs

import {Pair} from "fp-kudojs";

const point1 = Pair(1, 2);
console.log(point1.toString()); // Pair((1), (2))

const point2 = Pair.of(2);
console.log(point2.toString()); // Pair((2), (2))

Two Pairs are equal if the first and the second element of each Pair are equal

console.log(point1.equals(Pair(1,2))) //true

console.log(point2.equals(point1) //false

console.log(Pair([1], [2]).equals(Pair([1],[2]))) //false

Concat Pairs. Note that to concat pairs the elements of each Pair should be semigroups of the same type

const p1 = Pair([1], [2]);
const p2 = Pair([3], [4]);

const p3 = p1.concat(p2);

console.log(p3.getValue()); // [[1, 3], [2, 4]] 

** TODO: Add more examples **