forked from DrBoolean/pointfree-fantasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointfree.js
133 lines (106 loc) · 2.8 KB
/
pointfree.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var curry = require('lodash.curry');
var BUILT_INS = { 'array': require('./instances/array')
, 'function': require('./instances/function')
, 'string': require('./instances/string')
}
var _groupsOf = curry(function(n, xs) {
if(!xs.length) return [];
return [xs.slice(0, n)].concat(_groupsOf(n, xs.slice(n, xs.length)));
});
var _compose = curry(function(f,g,x) { return f(g(x)) });
var I = function(x){ return x; }
// f . g . h == compose(f, g, h)
var toAssociativeCommaInfix = function(fn) {
return function() {
var fns = [].slice.call(arguments)
return function() {
return _groupsOf(2, fns).reverse().map(function(g) {
return (g.length > 1) ? fn.apply(this,g) : g[0];
}).reduce(function(x, f) {
return [f.apply(f,x)];
}, arguments)[0];
};
};
};
var compose = toAssociativeCommaInfix(_compose);
var pointy = {};
var id = function(x) { return x; }
var K = function(x) { return function(){ return x; } }
var map = curry(function(f, u) {
return u.fmap ? u.fmap(f) : u.map(f); //sometimes map passes index so we use fmap if it has it.
});
var ap = curry(function(a1, a2) {
return a1.ap(a2);
});
var liftA2 = curry(function(f, x, y) {
return map(f,x).ap(y);
});
var liftA3 = curry(function(f, x, y, z) {
return map(f, x).ap(y).ap(z);
});
var chain = curry(function(f, mv) {
return mv.chain(f);
});
var mjoin = function(mmv) {
return chain(id, mmv);
};
var concat = curry(function(x, y) {
return x.concat(y);
});
var mconcat = function(xs, empty) {
return xs.length ? xs.reduce(concat) : empty();
};
var sequenceA = curry(function(point, fctr) {
return fctr.traverse(id, point);
});
var of = function(x) {
return x.of;
};
var traverse = curry(function(f, point, fctr) {
return compose(sequenceA(point), map(f))(fctr);
});
var foldMap = curry(function(f, fldable) {
return fldable.reduce(function(acc, x) {
var r = f(x);
acc = acc || r.empty();
return acc.concat(r);
}, null);
});
var fold = foldMap(I);
var toList = function(x) {
return x.reduce(function(acc, y) {
return [y].concat(acc);
}, []);
};
var expose = function(env) {
var f;
for (f in pointy) {
if (f !== 'expose' && pointy.hasOwnProperty(f)) {
env[f] = pointy[f];
}
}
}
pointy.I = id;
pointy.K = K;
pointy.compose = compose;
pointy.curry = curry; //lodash curry
pointy.of = of;
pointy.map = map;
pointy.ap = ap;
pointy.liftA2 = liftA2;
pointy.liftA3 = liftA3;
pointy.chain = chain;
pointy.mjoin = mjoin;
pointy.concat = concat;
pointy.mappend = concat;
pointy.mconcat = mconcat;
pointy.sequenceA = sequenceA;
pointy.traverse = traverse;
pointy.foldMap = foldMap;
pointy.fold = fold;
pointy.toList = toList;
pointy.expose = expose;
module.exports = pointy;
if(typeof window == "object") {
pointfree = pointy;
}