This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
93 lines (82 loc) · 1.85 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function identity(a) {
return a;
}
exports.identity = identity;
function append(a, b) {
return a.concat(b);
}
exports.append = append;
function empty(m) {
return m.empty ? m.empty() : m.constructor.empty();
}
exports.empty = empty;
function map(m, f) {
if(m.map) return m.map(f);
return flatMap(m, function(a) {
return point(m, f(a));
});
}
exports.map = map;
function flatMap(m, f) {
return m.chain ? m.chain(f) : m.then(f);
}
exports.flatMap = flatMap;
function point(m, a) {
return m.of ? m.of(a) : m.constructor.of(a);
}
exports.point = point;
function join(m) {
return flatMap(m, identity);
}
exports.join = join;
function ap(a, f) {
if(f.ap) return f.ap(a);
return flatMap(f, function(f) {
return map(a, f);
});
}
exports.ap = ap;
function lift2(f, a, b) {
return ap(b, map(a, function(a) {
return function(b) {
return f(a, b);
};
}));
}
exports.lift2 = lift2;
function lift3(f, a, b, c) {
return ap(c, ap(b, map(a, function(a) {
return function(b) {
return function(c) {
return f(a, b, c);
};
};
})));
}
exports.lift3 = lift3;
function lift4(f, a, b, c, d) {
return ap(d, ap(c, ap(b, map(a, function(a) {
return function(b) {
return function(c) {
return function(d) {
return f(a, b, c, d);
};
};
};
}))));
}
exports.lift4 = lift4;
function lift5(f, a, b, c, d, e) {
return ap(e, ap(d, ap(c, ap(b, map(a, function(a) {
return function(b) {
return function(c) {
return function(d) {
return function(e) {
return f(a, b, c, d, e);
};
};
};
};
})))));
}
exports.lift5 = lift5;