-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pair.js
41 lines (32 loc) · 940 Bytes
/
Pair.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { curry } from "https://x.nest.land/ramda@0.27.0/source/index.js";
import { factorizeType } from "./factories.js";
/**
* The `Pair` type represents two values.
*
* The `Pair` type implements the following algebras:
* - [x] Bifunctor
* - [x] Functor
*
* ### Example
*
* ```js
* const pair = Pair(42, 42)
* .bimap(
* x => x * 2,
* x => x + 2
* );
*
* assert(Pair.is(pair));
* assert(pair.first === 84);
* assert(pair.first === 44);
* ```
*/
export const Pair = factorizeType("Pair", [ "first", "second" ]);
Pair.prototype.bimap = Pair.prototype["fantasy-land/bimap"] = function (unaryFunctionA, unaryFunctionB) {
return Pair(unaryFunctionA(this.first), unaryFunctionB(this.second));
};
Pair.prototype.map = Pair.prototype["fantasy-land/map"] = function (unaryFunction) {
return Pair(unaryFunction(this.first), this.second);
};
export const factorizePair = curry(Pair);
export default Pair;