-
Notifications
You must be signed in to change notification settings - Fork 117
/
underscore.function.iterators.js
345 lines (310 loc) · 7.12 KB
/
underscore.function.iterators.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Underscore-contrib (underscore.function.iterators.js 0.3.0)
// (c) 2013 Michael Fogus and DocumentCloud Inc.
// Underscore-contrib may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `require` it on the server.
if (typeof exports === 'object') {
_ = module.exports = require('underscore');
}
// Helpers
// -------
var HASNTBEENRUN = {};
function unary (fun) {
return function (first) {
return fun.call(this, first);
};
}
function binary (fun) {
return function (first, second) {
return fun.call(this, first, second);
};
}
// Mixing in the iterator functions
// --------------------------------
function foldl (iter, binaryFn, seed) {
var state, element;
if (seed !== void 0) {
state = seed;
}
else {
state = iter();
}
element = iter();
while (element != null) {
state = binaryFn.call(element, state, element);
element = iter();
}
return state;
}
function unfold (seed, unaryFn) {
var state = HASNTBEENRUN;
return function () {
if (state === HASNTBEENRUN) {
state = seed;
} else if (state != null) {
state = unaryFn.call(state, state);
}
return state;
};
}
// note that the unfoldWithReturn behaves differently than
// unfold with respect to the first value returned
function unfoldWithReturn (seed, unaryFn) {
var state = seed,
pair,
value;
return function () {
if (state != null) {
pair = unaryFn.call(state, state);
value = pair[1];
state = value != null ? pair[0] : void 0;
return value;
}
else return void 0;
};
}
function accumulate (iter, binaryFn, initial) {
var state = initial;
return function () {
var element = iter();
if (element == null) {
return element;
}
else {
if (state === void 0) {
state = element;
} else {
state = binaryFn.call(element, state, element);
}
return state;
}
};
}
function accumulateWithReturn (iter, binaryFn, initial) {
var state = initial,
stateAndReturnValue,
element;
return function () {
element = iter();
if (element == null) {
return element;
}
else {
if (state === void 0) {
state = element;
return state;
}
else {
stateAndReturnValue = binaryFn.call(element, state, element);
state = stateAndReturnValue[0];
return stateAndReturnValue[1];
}
}
};
}
function map (iter, unaryFn) {
return function() {
var element;
element = iter();
if (element != null) {
return unaryFn.call(element, element);
} else {
return void 0;
}
};
}
function mapcat(iter, unaryFn) {
var lastIter = null;
return function() {
var element;
var gen;
if (lastIter == null) {
gen = iter();
if (gen == null) {
lastIter = null;
return void 0;
}
lastIter = unaryFn.call(gen, gen);
}
while (element == null) {
element = lastIter();
if (element == null) {
gen = iter();
if (gen == null) {
lastIter = null;
return void 0;
}
else {
lastIter = unaryFn.call(gen, gen);
}
}
}
return element;
};
}
function select (iter, unaryPredicateFn) {
return function() {
var element;
element = iter();
while (element != null) {
if (unaryPredicateFn.call(element, element)) {
return element;
}
element = iter();
}
return void 0;
};
}
function reject (iter, unaryPredicateFn) {
return select(iter, function (something) {
return !unaryPredicateFn(something);
});
}
function find (iter, unaryPredicateFn) {
return select(iter, unaryPredicateFn)();
}
function slice (iter, numberToDrop, numberToTake) {
var count = 0;
while (numberToDrop-- > 0) {
iter();
}
if (numberToTake != null) {
return function() {
if (++count <= numberToTake) {
return iter();
} else {
return void 0;
}
};
}
else return iter;
}
function drop (iter, numberToDrop) {
return slice(iter, numberToDrop == null ? 1 : numberToDrop);
}
function take (iter, numberToTake) {
return slice(iter, 0, numberToTake == null ? 1 : numberToTake);
}
function List (array) {
var index = 0;
return function() {
return array[index++];
};
}
function cycle(array) {
var index = 0,
length = array.length;
return function() {
return array[index++ % length];
};
}
function Tree (array) {
var index, myself, state;
index = 0;
state = [];
myself = function() {
var element, tempState;
element = array[index++];
if (element instanceof Array) {
state.push({
array: array,
index: index
});
array = element;
index = 0;
return myself();
} else if (element === void 0) {
if (state.length > 0) {
tempState = state.pop();
array = tempState.array;
index = tempState.index;
return myself();
} else {
return void 0;
}
} else {
return element;
}
};
return myself;
}
function K (value) {
return function () {
return value;
};
}
function upRange (from, to, by) {
return function () {
var was;
if (from > to) {
return void 0;
}
else {
was = from;
from = from + by;
return was;
}
};
}
function downRange (from, to, by) {
return function () {
var was;
if (from < to) {
return void 0;
}
else {
was = from;
from = from - by;
return was;
}
};
}
function range (from, to, by) {
if (from == null) {
return upRange(1, Infinity, 1);
}
else if (to == null) {
return upRange(from, Infinity, 1);
}
else if (by == null) {
if (from <= to) {
return upRange(from, to, 1);
}
else return downRange(from, to, 1);
}
else if (by > 0) {
return upRange(from, to, by);
}
else if (by < 0) {
return downRange(from, to, Math.abs(by));
}
else return k(from);
}
var numbers = unary(range);
_.iterators = {
accumulate: accumulate,
accumulateWithReturn: accumulateWithReturn,
foldl: foldl,
reduce: foldl,
unfold: unfold,
unfoldWithReturn: unfoldWithReturn,
map: map,
mapcat: mapcat,
select: select,
reject: reject,
filter: select,
find: find,
slice: slice,
drop: drop,
take: take,
List: List,
Tree: Tree,
constant: K,
K: K,
numbers: numbers,
range: range,
cycle: cycle
};
}).call(this, void 0);