-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfp.js
204 lines (148 loc) · 5.48 KB
/
fp.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var FP = (function () {
var Module = Object.create(null);
Object.assign(BaseModule, {
forEach: function (f, xs) {
for (var i = 0; i < xs.length; i+=1) {
var x = xs[i];
f(x);
}
},
map: function (f, xs) {
var ys = [];
Module.forEach(function (x) {
var y = f(x);
ys.push(y);
}, xs);
return ys;
},
keys: function (object) {
var keys = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
},
get: function (key, object) {
return object[key];
},
toPairs: function (object) {
return Module.keys(object)
.map(function covertToPair(key) {
var value = Module.get(key, object);
return [key, value];
});
},
set: function (key, value, object) {
object[key] = value;
return object;
},
fromPairs: function (pairs) {
var newObject = {};
Module.forEach(function (pair) {
var key = pair[0],
value = pair[1];
Module.set(key, value, object);
}, pairs);
return newObject;
},
mapObject: function (f, object) {
var mappedPairs = Module.toPairs(object) // Step 1: Convert to pairs
.map(function (pair) {
var key = pair[0],
value = pair[1],
newValue = f(value, key); // Step 2: Map over the value
return [key, newValue];
});
return Module.fromPairs(mappedPairs); // Step 3: Convert to object
},
filter: function (p, xs) {
var ys = [];
Module.forEach(function _checkPredicate(x) {
var y = x; // Just to get the name right with ys
if (p(x)) {
ys.push(y);
}
});
return ys;
},
filterObject: function (p, object) {
var filteredPairs = Module.toPairs(object) // Just like mapObject
.filter(function (pair) { // Notice map is just replaced with filter
var key = pair[0],
value = pair[1];
return p(value, key); // We apply the predicate here
});
return Module.fromPairs(filteredPairs);
},
reduce: function (oper, initialValue, xs) {
var currentValue = initialValue;
Module.forEach(function combine(x) {
currentValue = oper(currentValue, x);
});
return totalValue;
},
doBefore: function (before, main) {
return function (/* args */) { // A function that combines functions
var args = [].slice.call(arguments);
before.apply(null, args); // Execute the function before
return main.apply(null, args); // Execute the main function
};
},
compose: function (g, f) {
return function (/* args */) {
var args = arguments;
var firstValue = f.apply(null, args),
nextValue = g.call(null, firstValue);
return nextValue;
};
},
reverse: function (xs) {
var nxs = xs.slice(); // Shortcut for copying an array
nxs.reverse(); // Mutates the array
return nxs;
},
first: function (xs) {
return Module.get(0, xs);
},
rest: function (xs) {
return xs.slice(1);
},
reduceFirst: function (oper, xs) {
var initialValue = Module.first(xs),
otherValues = Module.rest(xs);
return Module.reduce(oper, initialValue, otherValues);
},
pipe: function (first, next) {
return Module.compose(next, first);
},
composes: function (/* fs */) {
var fs = [].slice.call(arguments);
return Module.reduceFirst(Module.pipe, Module.reverse(fs));
},
pipes: function (/* fs */) {
var fs = [].slice.call(arguments);
return Module.reduceFirst(Module.pipe, fs);
},
curry: function (f) {
// Get the number of arguments in a function
var numberOfArgs = f.length;
// An helper function to curry the original function
var currier = function _recursiveCurry(previousArgs) {
return function _curried(/* currentArgs */) {
var currentArgs = [].slice.call(arguments),
newArgs = previousArgs.concat(currentArgs);
// If there is enough args, invoke the function
if (newArgs.length >= numberOfArgs) {
return f.apply(null, arguments);
} else { // If there is not enough args yet, keep currying
return _recursiveCurry(newArgs);
}
};
};
return currier([]); // Start recursion with no arguments
}
});
return Object.freeze(Module);
}());