-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathƒ.js
238 lines (211 loc) · 4.73 KB
/
ƒ.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict';
/**
* Function composition
* @param ...fs functions to compose
* @return composed function
**/
export function comp (...fs) {
return (v, ...args) =>
fs.reduceRight(
(g, f) => f(g, ...args), v
);
}
/**
* Flip function arguments
* @param f function to flip
* @return f applied with args in reverse order
**/
export function flip (f) {
return (a, b, ...args) =>
f(b, a, ...args);
}
/**
* Applies a function which is passed as the second argument to
* the third argument and it comapares the result with the condition,
* if the condition evaluates to true, it prints the result, if not,
* it passes the result to the function and repeats the cycle as long
* as the condition is matched
* @param condition condition to be applied to f
* @param f function to match against
* @return result if condition is true else repeat cycle
**/
export function until (condition, f){
return (...args) => {
var r = f(...args);
return condition(r) ? r : until(condition, f)(r);
};
}
/**
* List operations
**/
export function head (xs) { return xs[0]; }
export function last (xs) { return xs[xs.length - 1]; }
export function tail (xs) { return xs.slice(1); }
export function init (xs) { return xs.slice(0, -1); }
/**
* Special folds
**/
export function concat (...xs) {
return xs.reduce(
(a, b) => a.concat(b)
);
}
export function concatMap (f, ...xs) {
return concat(xs.map(f));
}
/**
* Returns an infinite list of repeated applications of f to x
* @param f function to iterate with
* @param x initial value
**/
export function iterate (f, x) {
return Proxy.create({
get: (_, property) => {
if (isNaN(property))
return Infinity;
var r = x;
for (var i = 0; i < property; i++)
r = f(r);
return r;
}
});
}
/**
* Returns an infinite list, with x the value of every element
* @param x value
**/
export function repeat (x) {
return Proxy.create({
get: (_, property) =>
isNaN(property) ?
Infinity : x
});
}
/**
* Ties a finite list into a circular one, or equivalently,
* the infinite repetition of the original list
* @param xs list to be cycled
**/
export function cycle (xs) {
return Proxy.create({
get: (_, property) =>
isNaN(property) ?
Infinity : xs[property % xs.length]
});
}
/**
* Applied to a list xs, returns the prefix of xs of length n
* @param n number of elements to take
* @param xs list to take from
**/
export function take (n, xs) {
if (n <= 0) return [];
if (n >= xs.length) return xs;
var r = [];
for (var i = 0; i < n; i++)
r.push(xs[i]);
return r;
}
/**
* Returns the suffix of xs after the first n elements
* @param n number of elements to drop
* @param xs list to drop from
**/
export function drop (n, xs) {
if (n <= 0) return xs;
if (n >= xs.length) return [];
if (xs.length === Infinity) {
return Proxy.create({
get: (_, property) =>
isNaN(property) ?
Infinity : xs[property + n]
});
}
var r = [];
for (var i = n; i < xs.length; i++)
r.push(xs[i]);
return r;
}
/**
* Returns a tuple where first element is xs prefix of length n and
* second element is the remainder of the list
* @param n index to split at
* @param xs list to split
**/
export function splitAt (n, xs) {
return [take(n, xs), drop(n, xs)];
}
/**
* Applied to a predicate f and a list xs, returns the longest
* prefix (possibly empty) of xs of elements that satisfy f
* @param f predicate to satisfy
* @param xs list to take from
**/
export function takeWhile (f, xs) {
var r = [];
for (var i = 0; i < xs.length; i++) {
if (!f(xs[i]))
return r;
r.push(xs[i]);
}
return r;
}
/**
* Returns the suffix remaining after takeWhile
* @param f predicate to satisfy
* @param xs list to drop from
**/
export function dropWhile (f, xs) {
return drop(takeWhile(f, xs).length, xs);
}
/**
* Zip two arrays into a list of n-ples
* @param ...xs arrays to zip
* @return a list of of n-ples
**/
export function zip (...xs) {
var r = [],
nple = [],
length = Math.min(...xs.map(x => x.length));
for (var i = 0; i < length; i++) {
xs.forEach(
x => nple.push(x[i])
);
r.push(nple);
nple = [];
}
return r;
}
/**
* Generalises zip by zipping with the function given
* as the first argument, instead of a tupling function.
* @param op function to zip with
* @param ...xs arrays to zip
* @return array zipped with the op function
**/
export function zipWith (op, ...xs) {
return zip(...xs).map(
x => x.reduce(op)
);
}
export default {
comp,
flip,
until,
head,
last,
tail,
init,
concat,
concatMap,
iterate,
repeat,
cycle,
take,
drop,
splitAt,
takeWhile,
dropWhile,
zip,
zipWith
};