forked from fantasyland/fantasy-land
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.js
60 lines (48 loc) · 1.19 KB
/
id.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
function Id(a) {
this.value = a;
}
// Setoid
Id.prototype.equals = function(b) {
return typeof this.value.equals === "function" ? this.value.equals(b.value) : this.value === b.value;
};
// Semigroup (value must also be a Semigroup)
Id.prototype.concat = function(b) {
return new Id(this.value.concat(b.value));
};
// Monoid (value must also be a Monoid)
Id.prototype.empty = function() {
return new Id(this.value.empty ? this.value.empty() : this.value.constructor.empty());
};
// Foldable
Id.prototype.reduce = function(f, acc) {
return f(acc, this.value);
};
// Functor
Id.prototype.map = function(f) {
return new Id(f(this.value));
};
// Apply
Id.prototype.ap = function(b) {
return new Id(this.value(b.value));
};
// Traversable
Id.prototype.sequence = function(f, of) {
return f(this.value).map(function(y){ return new Id(y); });
};
// Chain
Id.prototype.chain = function(f) {
return f(this.value);
};
// Extend
Id.prototype.extend = function(f) {
return new Id(f(this));
};
// Applicative
Id.of = function(a) {
return new Id(a);
};
// Comonad
Id.prototype.extract = function() {
return this.value;
};
if (typeof module == 'object') module.exports = Id;