forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.js
371 lines (327 loc) · 10.3 KB
/
iterator.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"use strict";
module.exports = Iterator;
var Object = require("./shim-object");
var GenericCollection = require("./generic-collection");
// upgrades an iterable to a Iterator
function Iterator(iterable) {
if (!(this instanceof Iterator)) {
return new Iterator(iterable);
}
if (Array.isArray(iterable) || typeof iterable === "string")
return Iterator.iterate(iterable);
iterable = Object(iterable);
if (iterable instanceof Iterator) {
return iterable;
} else if (iterable.next) {
this.next = function () {
return iterable.next();
};
} else if (iterable.iterate) {
var iterator = iterable.iterate();
this.next = function () {
return iterator.next();
};
} else if (Object.prototype.toString.call(iterable) === "[object Function]") {
this.next = iterable;
} else {
throw new TypeError("Can't iterate " + iterable);
}
}
Iterator.prototype.forEach = GenericCollection.prototype.forEach;
Iterator.prototype.map = GenericCollection.prototype.map;
Iterator.prototype.filter = GenericCollection.prototype.filter;
Iterator.prototype.every = GenericCollection.prototype.every;
Iterator.prototype.some = GenericCollection.prototype.some;
Iterator.prototype.any = GenericCollection.prototype.any;
Iterator.prototype.all = GenericCollection.prototype.all;
Iterator.prototype.min = GenericCollection.prototype.min;
Iterator.prototype.max = GenericCollection.prototype.max;
Iterator.prototype.sum = GenericCollection.prototype.sum;
Iterator.prototype.average = GenericCollection.prototype.average;
Iterator.prototype.flatten = GenericCollection.prototype.flatten;
Iterator.prototype.zip = GenericCollection.prototype.zip;
Iterator.prototype.enumerate = GenericCollection.prototype.enumerate;
Iterator.prototype.sorted = GenericCollection.prototype.sorted;
Iterator.prototype.group = GenericCollection.prototype.group;
Iterator.prototype.reversed = GenericCollection.prototype.reversed;
Iterator.prototype.toArray = GenericCollection.prototype.toArray;
Iterator.prototype.toObject = GenericCollection.prototype.toObject;
Iterator.prototype.iterator = GenericCollection.prototype.iterator;
// this is a bit of a cheat so flatten and such work with the generic
// reducible
Iterator.prototype.constructClone = function (values) {
var clone = [];
clone.addEach(values);
return clone;
};
Iterator.prototype.mapIterator = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1],
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return new self.constructor(function () {
return callback.call(thisp, self.next(), i++, self);
});
};
Iterator.prototype.filterIterator = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1],
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return new self.constructor(function () {
var value;
while (true) {
value = self.next();
if (callback.call(thisp, value, i++, self))
return value;
}
});
};
Iterator.prototype.reduce = function (callback /*, initial, thisp*/) {
var self = Iterator(this),
result = arguments[1],
thisp = arguments[2],
i = 0,
value;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
// first iteration unrolled
try {
value = self.next();
if (arguments.length > 1) {
result = callback.call(thisp, result, value, i, self);
} else {
result = value;
}
i++;
} catch (exception) {
if (isStopIteration(exception)) {
if (arguments.length > 1) {
return arguments[1]; // initial
} else {
throw TypeError("cannot reduce a value from an empty iterator with no initial value");
}
} else {
throw exception;
}
}
// remaining entries
try {
while (true) {
value = self.next();
result = callback.call(thisp, result, value, i, self);
i++;
}
} catch (exception) {
if (isStopIteration(exception)) {
return result;
} else {
throw exception;
}
}
};
Iterator.prototype.concat = function () {
return Iterator.concat(
Array.prototype.concat.apply(this, arguments)
);
};
Iterator.prototype.dropWhile = function (callback /*, thisp */) {
var self = Iterator(this),
thisp = arguments[1],
stopped = false,
stopValue;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
self.forEach(function (value, i) {
if (!callback.call(thisp, value, i, self)) {
stopped = true;
stopValue = value;
throw StopIteration;
}
});
if (stopped) {
return self.constructor([stopValue]).concat(self);
} else {
return self.constructor([]);
}
};
Iterator.prototype.takeWhile = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1];
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return self.mapIterator(function (value, i) {
if (!callback.call(thisp, value, i, self))
throw StopIteration;
return value;
});
};
Iterator.prototype.zipIterator = function () {
return Iterator.unzip(
Array.prototype.concat.apply(this, arguments)
);
};
Iterator.prototype.enumerateIterator = function (start) {
return Iterator.count(start).zipIterator(this);
};
// creates an iterator for Array and String
Iterator.iterate = function (iterable) {
var start;
start = 0;
return new Iterator(function () {
// advance to next owned entry
if (typeof iterable === "object") {
while (!(start in iterable)) {
// deliberately late bound
if (start >= iterable.length)
throw StopIteration;
start += 1;
}
} else if (start >= iterable.length) {
throw StopIteration;
}
var result = iterable[start];
start += 1;
return result;
});
};
Iterator.cycle = function (cycle, times) {
if (arguments.length < 2)
times = Infinity;
//cycle = Iterator(cycle).toArray();
var next = function () {
throw StopIteration;
};
return new Iterator(function () {
var iteration;
try {
return next();
} catch (exception) {
if (isStopIteration(exception)) {
if (times <= 0)
throw exception;
times--;
iteration = Iterator.iterate(cycle);
next = iteration.next.bind(iteration);
return next();
} else {
throw exception;
}
}
});
};
Iterator.concat = function (iterators) {
iterators = Iterator(iterators);
var next = function () {
throw StopIteration;
};
return new Iterator(function (){
var iteration;
try {
return next();
} catch (exception) {
if (isStopIteration(exception)) {
iteration = Iterator(iterators.next());
next = iteration.next.bind(iteration);
return next();
} else {
throw exception;
}
}
});
};
Iterator.unzip = function (iterators) {
iterators = Iterator(iterators).map(Iterator);
if (iterators.length === 0)
return new Iterator([]);
return new Iterator(function () {
var stopped;
var result = iterators.map(function (iterator) {
try {
return iterator.next();
} catch (exception) {
if (isStopIteration(exception)) {
stopped = true;
} else {
throw exception;
}
}
});
if (stopped) {
throw StopIteration;
}
return result;
});
};
Iterator.zip = function () {
return Iterator.unzip(
Array.prototype.slice.call(arguments)
);
};
Iterator.chain = function () {
return Iterator.concat(
Array.prototype.slice.call(arguments)
);
};
Iterator.range = function (start, stop, step) {
if (arguments.length < 3) {
step = 1;
}
if (arguments.length < 2) {
stop = start;
start = 0;
}
start = start || 0;
step = step || 1;
return new Iterator(function () {
if (start >= stop)
throw StopIteration;
var result = start;
start += step;
return result;
});
};
Iterator.count = function (start, step) {
return Iterator.range(start, Infinity, step);
};
Iterator.repeat = function (value, times) {
return new Iterator.range(times).mapIterator(function () {
return value;
});
};
// shim isStopIteration
if (typeof isStopIteration === "undefined") {
global.isStopIteration = function (exception) {
return Object.prototype.toString.call(exception) === "[object StopIteration]";
};
}
// shim StopIteration
if (typeof StopIteration === "undefined") {
global.StopIteration = {};
Object.prototype.toString = (function (toString) {
return function () {
if (
this === global.StopIteration ||
this instanceof global.ReturnValue
)
return "[object StopIteration]";
else
return toString.call(this, arguments);
};
})(Object.prototype.toString);
}
// shim ReturnValue
if (typeof ReturnValue === "undefined") {
global.ReturnValue = function ReturnValue(value) {
this.message = "Iteration stopped with " + value;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ReturnValue);
}
if (!(this instanceof global.ReturnValue))
return new global.ReturnValue(value);
this.value = value;
};
ReturnValue.prototype = Error.prototype;
}