-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathTuple.js
68 lines (55 loc) · 1.38 KB
/
Tuple.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var toString = require('ramda/src/toString');
var equals = require('ramda/src/equals');
function Tuple(x, y) {
switch (arguments.length) {
case 0:
throw new TypeError('no arguments to Tuple');
case 1:
return function(y) {
return new _Tuple(x, y);
};
default:
return new _Tuple(x, y);
}
}
function _Tuple(x, y) {
this[0] = x;
this[1] = y;
this.length = 2;
}
function ensureConcat(xs) {
xs.forEach(function(x) {
if (typeof x.concat != 'function') {
throw new TypeError(toString(x) + ' must be a semigroup to perform this operation');
}
});
}
Tuple.fst = function(x) {
return x[0];
};
Tuple.snd = function(x) {
return x[1];
};
_Tuple.prototype['@@type'] = 'ramda-fantasy/Tuple';
// semigroup
_Tuple.prototype.concat = function(x) {
ensureConcat([this[0], this[1]]);
return Tuple(this[0].concat(x[0]), this[1].concat(x[1]));
};
// functor
_Tuple.prototype.map = function(f) {
return Tuple(this[0], f(this[1]));
};
// apply
_Tuple.prototype.ap = function(m) {
ensureConcat([this[0]]);
return Tuple(this[0].concat(m[0]), this[1](m[1]));
};
// setoid
_Tuple.prototype.equals = function(that) {
return that instanceof _Tuple && equals(this[0], that[0]) && equals(this[1], that[1]);
};
_Tuple.prototype.toString = function() {
return 'Tuple(' + toString(this[0]) + ', ' + toString(this[1]) + ')';
};
module.exports = Tuple;