/
LazyIterator.js
1361 lines (1178 loc) · 34.8 KB
/
LazyIterator.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Class for lazy iteration.
* Wraps around an iterator.
* @class
*/
class LazyIterator {
/**
* Wraps a lazy iterator around an iterator.
* @param {Iterator} iterator Object that implements JavaScript's iterator protocol.
*/
constructor(iterator) {
this.iterator = iterator;
this.peeked = false;
this.peekedAt = null;
}
[Symbol.iterator]() {
return this;
}
/**
* Returns the next item in the iterator.
* @returns {IteratorResult} Iterator item.
*/
next() {
if (this.peeked) {
this.peeked = false;
return this.peekedAt;
}
return this.iterator.next();
}
/**
* Peeks at the next item in the iterator.
* @returns {IteratorResult} Iterator item.
*/
peek() {
if (this.peeked) {
return this.peekedAt;
}
this.peeked = true;
this.peekedAt = this.iterator.next();
return this.peekedAt;
}
/**
* Returns the value at a certain position.
* This consumes the iterator until the given position.
* @param {number} index Position of value.
* @returns {any} The value at the given position.
*/
at(index) {
let value;
for (let i = 0; i <= index; i++) {
const item = this.next();
if (item.done) {
return undefined;
}
value = item.value;
}
return value;
}
/**
* Returns the size of the iterator.
* This consumes the iterator.
* @returns {number} Size of the iterator.
*/
count() {
let i = 0;
// eslint-disable-next-line no-unused-vars
for (const value of this) {
i++;
}
return i;
}
/**
* Returns the last item of the iterator.
* This consumes the iterator.
* @returns {any} Last item of the iterator.
*/
last() {
let val;
for (const value of this) {
val = value;
}
return val;
}
/**
* Returns an iterator that steps in an interval.
* The iterator starts at the first element.
* @param {number} stepSize Interval to step by.
* @returns {StepIterator} The iterator.
*/
stepBy(stepSize) {
return new StepIterator(this, stepSize);
}
/**
* Returns an iterator that skips some amount of elements.
* @param {number} skipAmount Amount of elements to skip.
* @returns {SkipIterator} The iterator.
*/
skip(skipAmount) {
return new SkipIterator(this, skipAmount);
}
/**
* Returns an iterator that takes only some amount of elements.
* @param {number} takeAmount Amount of elements to take.
* @returns {TakeIterator} The iterator.
*/
take(takeAmount) {
return new TakeIterator(this, takeAmount);
}
/**
* Returns an iterator that skips while a condition is true.
* The iterator will skip until the predicate returns false.
* After that, all elements will be yielded as normal.
* @param {Predicate} fn Predicate function.
* @returns {SkipWhileIterator} The iterator.
*/
skipWhile(fn) {
return new SkipWhileIterator(this, fn);
}
/**
* Returns an iterator that takes while a condition is true.
* The iterator will take until the predicate returns false.
* After that, the iterator is considered done.
* @param {Predicate} fn Predicate function.
* @returns {TakeWhileIterator} The iterator.
*/
takeWhile(fn) {
return new TakeWhileIterator(this, fn);
}
/**
* Returns an iterator that puts multiple consecutive values into one value.
* If the iterator does not divide evenly into the given size, there will be a shorter chunk at the end.
* @param {number} chunkSize Size of a chunk.
* @returns {ChunkIterator} The iterator.
*/
chunk(chunkSize) {
return new ChunkIterator(this, chunkSize);
}
/**
* Returns an iterator that includes the index.
* The iterator will iterate through (index, value) pairs.
* @returns {EnumerateIterator} The iterator.
*/
enumerate() {
return new EnumerateIterator(this);
}
/**
* Returns an iterator that is a chain of iterators.
* Once one iterator is done, the next iterator starts.
* @param {Array<Iterator|Iterable>} iters Iterators or iterables.
* @returns {ConcatIterator} The iterator.
*/
concat(...iters) {
return new ConcatIterator(this, iters.map(iter => LazyIterator.from(iter)));
}
/**
* Returns an iterator that repeats this iterator forever.
* @returns {CycleIterator} The iterator.
*/
cycle() {
return new CycleIterator(this);
}
/**
* Returns an iterator that maps each element with a function.
* @param {Mapping} fn Mapping function.
* @returns {MapIterator} The iterator.
*/
map(fn) {
return new MapIterator(this, fn);
}
/**
* Returns an iterator that filters out certain elements.
* @param {Predicate} fn Predicate function.
* @returns {FilterIterator} The iterator.
*/
filter(fn) {
return new FilterIterator(this, fn);
}
/**
* Returns an iterator that holds internal state.
* Each element of the iterator is the state at that iteration.
* This can be thought of as a reduce.
* @param {Reducer} fn Reducer function.
* @param {any} [accum] Accumulator.
* @returns {ScanIterator} The iterator.
*/
scan(fn, accum) {
return new ScanIterator(this, fn, accum);
}
/**
* Returns an iterator where each iterator is zipped with each other.
* Each element is a tuple of zipped elements e.g. (a, b, c) if given two iterators.
* @param {Array<Iterator|Iterable>} iters Iterators or iterables.
* @returns {ZipIterator} The iterator.
*/
zip(...iters) {
return new ZipIterator(this, iters.map(iter => LazyIterator.from(iter)));
}
/**
* Returns an iterator that flattens iterators and iterables inside this interator.
* @param {number} [depth=1] The amount of depth to flatten.
* @returns {FlatIterator} The iterator.
*/
flat(depth = 1) {
return new FlatIterator(this, depth);
}
/**
* Returns an iterator that flattens iterators and iterables from a mapping.
* @param {Mapping} fn Mapping function.
* @returns {FlatMapIterator} The iterator.
*/
flatMap(fn) {
return new FlatMapIterator(this, fn);
}
/**
* Returns an iterator where between every element is the given value.
* @param {any} val Value to join with.
* @returns {JoinIterator} The iterator.
*/
join(val) {
return new JoinIterator(this, val);
}
/**
* Returns an iterator where between every element are the values of the given iterator.
* The joining iterator will be consumed and cycled through.
* @param {Iterator|Iterable} iter Iterator to intercalate.
* @returns {JoinWithIterator} The iterator.
*/
joinWith(iter) {
return new JoinWithIterator(this, LazyIterator.from(iter));
}
/**
* Returns an iterator that calls a function on each element.
* @param {Consumer} fn Consumer function.
* @returns {EachIterator} The iterator.
*/
each(fn) {
return new EachIterator(this, fn);
}
/**
* Calls a function on each element.
* This consumes the iterator.
* @param {Consumer} fn Consumer function.
* @returns {void} Nothing.
*/
forEach(fn) {
for (const value of this) {
fn(value);
}
}
/**
* Reduces each element into one single result.
* Also known as fold, inject, etc.
* This consumes the iterator.
* @param {Reducer} fn Reducer function.
* @param {any} [accum] The accumulator.
* Defaults to the first element in the iterator.
* @returns {any} The accumulator.
*/
reduce(fn, accum) {
if (accum === undefined) {
const first = this.next();
if (first.done) {
throw new TypeError('Reduce of empty sequence with no initial value');
}
accum = first.value;
}
for (const value of this) {
accum = fn(accum, value);
}
return accum;
}
/**
* Takes the conjunction (logical AND) of every element.
* An empty iterator will return true.
* This consumes the iterator.
* @returns {any}
*/
and() {
return this.reduce((a, b) => a && b, true);
}
/**
* Takes the disjunction (logical OR) of every element.
* An empty iterator will return false.
* This consumes the iterator.
* @returns {any}
*/
or() {
return this.reduce((a, b) => a || b, false);
}
/**
* Sums or concatenates the elements in the iterator.
* This consumes the iterator.
* @returns {number|string} The sum or string.
*/
sum() {
return this.reduce((a, b) => a + b, 0);
}
/**
* Takes the product of the elements in the iterator.
* This consumes the iterator.
* @returns {number} The product.
*/
product() {
return this.reduce((a, b) => a * b, 1);
}
/**
* Finds a value in the iterator that passes the predicate.
* This consumes the iterator until the found value.
* @param {Predicate} fn Predicate function.
* @returns {any} The found value.
*/
find(fn) {
for (const value of this) {
if (fn(value)) {
return value;
}
}
return undefined;
}
/**
* Finds the index of the value in the iterator that passes the predicate.
* This consumes the iterator until the found value.
* @param {Predicate} fn Predicate function.
* @returns {number} The found index.
*/
findIndex(fn) {
let i = 0;
for (const value of this) {
if (fn(value)) {
return i;
}
i++;
}
return -1;
}
/**
* Checks if a value is in the iterator.
* This consumes the iterator until the found value.
* @param {any} searchElement Value to look for inclusion.
* @param {number} [from=0] Index to search from.
* @returns {boolean} Whether the value was found.
*/
includes(searchElement, from = 0) {
let i = 0;
for (const value of this) {
if (i < from) {
i++;
continue;
}
if (sameValueZero(searchElement, value)) {
return true;
}
}
return false;
}
/**
* Checks that every element in the iterator passes the predicate.
* This consumes the iterator until a value that does not pass the predicate.
* @param {Predicate} fn Predicate function.
* @returns {boolean} Whether all elements passed or not.
*/
every(fn) {
for (const value of this) {
if (!fn(value)) {
return false;
}
}
return true;
}
/**
* Checks that one element in the iterator passes the predicate.
* This consumes the iterator until a value passes the predicate.
* @param {Predicate} fn Predicate function.
* @returns {boolean} Whether one element passed or not.
*/
some(fn) {
for (const value of this) {
if (fn(value)) {
return true;
}
}
return false;
}
/**
* Finds the element that is considered the maximum.
* A mapping can be passed to compare by an associated value.
* This consumes the iterator.
* @param {Mapping} [fn] Mapping function.
* @returns {any} The maximum.
*/
max(fn = x => x) {
let max;
const first = this.next();
if (first.done) {
return undefined;
}
max = first.value;
for (const value of this) {
if (fn(value) > fn(max)) {
max = value;
}
}
return max;
}
/**
* Finds the element that is considered the minimum.
* A mapping can be passed to compare by an associated value.
* This consumes the iterator.
* @param {Mapping} [fn] Mapping function.
* @returns {any} The minimum.
*/
min(fn = x => x) {
let min;
const first = this.next();
if (first.done) {
return undefined;
}
min = first.value;
for (const value of this) {
if (fn(value) < fn(min)) {
min = value;
}
}
return min;
}
/**
* Finds the element that is considered the maximum by some comparison.
* This consumes the iterator.
* @param {Comparator} fn Comparator function.
* @returns {any} The maximum.
*/
maxBy(fn) {
let max;
const first = this.next();
if (first.done) {
return undefined;
}
max = first.value;
for (const value of this) {
if (fn(value, max) > 0) {
max = value;
}
}
return max;
}
/**
* Finds the element that is considered the minimum by some comparison.
* This consumes the iterator.
* @param {Comparator} fn Comparator function.
* @returns {any} The minimum.
*/
minBy(fn) {
let min;
const first = this.next();
if (first.done) {
return undefined;
}
min = first.value;
for (const value of this) {
if (fn(value, min) < 0) {
min = value;
}
}
return min;
}
/**
* Collects the iterator into a collection.
* Defaults to an array.
* This consumes the iterator.
* @param {CollectionConstructor} [cons] Constructs a collection.
* @param {CollectionExtender} [extend] Extends a collection.
* @returns {any} The collection.
*/
collect(cons = consFunctions.Array, extend = extendFunctions.Array) {
let coll = cons();
for (const value of this) {
coll = extend(coll, value);
}
return coll;
}
/**
* Partitions the iterator into a collection based on a predicate.
* Items passing the predicate goes in the first collection.
* Items not passing goes into the second collection.
* The kind of collection defaults to an array.
* This consumes the iterator.
* @param {Predicate} fn Predicate fuction.
* @param {CollectionConstructor} [cons] Constructs a collection.
* @param {CollectionExtender} [extend] Extends a collection.
* @returns {[any, any]} The collections.
*/
partition(fn, cons = consFunctions.Array, extend = extendFunctions.Array) {
let left = cons();
let right = cons();
for (const value of this) {
if (fn(value)) {
left = extend(left, value);
} else {
right = extend(right, value);
}
}
return [left, right];
}
/**
* Unzips an iterator of tuples into collections.
* Each element in a tuple is added to the collection corresponding to their position.
* Can be thought of as the opposite of zip.
* The kind of collection defaults to an array.
* This consumes the iterator.
* @param {number} [size] Size of the tuples.
* Defaults to the length of the first element.
* @param {CollectionConstructor} [cons] Constructs a collection.
* @param {CollectionExtender} [extend] Extends a collection.
* @returns {any[]} The collections.
*/
unzip(size, cons = consFunctions.Array, extend = extendFunctions.Array) {
const first = this.next();
let length;
if (first.done) {
if (size === undefined) {
throw new TypeError('Unzip of empty sequence with no given size');
}
length = size;
} else {
length = size === undefined
? first.value.length
: size;
}
const colls = Array.from({ length }, () => cons());
if (!first.done) {
for (let i = 0; i < length; i++) {
colls[i] = extend(colls[i], first.value[i]);
}
}
for (const value of this) {
for (let i = 0; i < length; i++) {
colls[i] = extend(colls[i], value[i]);
}
}
return colls;
}
/**
* Groups consecutive elements that are equal into an array of collections.
* The kind of collection defaults to an array.
* This consumes the iterator.
* @param {Equality} [eq] Equality function.
* @param {CollectionConstructor} [cons] Constructs a collection.
* @param {CollectionExtender} [extend] Extends a collection.
* @returns {any[]}
*/
group(eq = (a, b) => sameValueZero(a, b), cons = consFunctions.Array, extend = extendFunctions.Array) {
const arr = [];
let accum = cons();
let prev = null;
const first = this.next();
if (first.done) {
return arr;
}
prev = first.value;
accum = extend(accum, first.value);
for (const value of this) {
if (eq(prev, value)) {
accum = extend(accum, value);
prev = value;
} else {
arr.push(accum);
accum = cons();
accum = extend(accum, value);
prev = value;
}
}
arr.push(accum);
return arr;
}
/**
* Categorizes elements by some property.
* The resulting map associates categories with collections of values.
* The kind of collection defaults to an array.
* This consumes the iterator.
* @param {Mapping} fn Mapping function.
* @param {CollectionConstructor} [cons] Constructs a collection.
* @param {CollectionExtender} [extend] Extends a collection.
* @returns {Map<any, any>}
*/
categorize(fn, cons = consFunctions.Array, extend = extendFunctions.Array) {
const map = new Map();
for (const value of this) {
const cat = fn(value);
if (!map.has(cat)) {
map.set(cat, cons());
}
map.set(cat, extend(map.get(cat), value));
}
return map;
}
/**
* Clones the iterator.
* This consumes the current iterator and recreates it.
* @returns {LazyIterator} The cloned iterator.
*/
clone() {
const cache = this.collectArray();
this.iterator = cache[Symbol.iterator]();
return new LazyIterator(cache[Symbol.iterator]());
}
/**
* Clones the iterator multiples times.
* This consumes the current iterator and recreates it.
* @param {number} amount Amount of times to clone.
* @returns {LazyIterator[]} The cloned iterators.
*/
cloneMany(amount) {
const cache = this.collectArray();
this.iterator = cache[Symbol.iterator]();
return Array.from({ length: amount }, () => new LazyIterator(cache[Symbol.iterator]()));
}
/**
* Checks if a value is an iterator.
* @param {any} val Value to check.
* @returns {boolean} Whether the value is an iterator.
*/
static isIterator(val) {
return typeof val.next === 'function';
}
/**
* Checks if a value is an iterable.
* @param {any} val Value to check.
* @returns {boolean} Whether the value is an iterable.
*/
static isIterable(val) {
return val != null && val[Symbol.iterator] != null;
}
/**
* Creates a lazy iterator from an iterator or iterable.
* @param {Iterator|Iterable} iter Iterator or iterable.
* @returns {LazyIterator} The iterator.
*/
static from(iter) {
if (LazyIterator.isIterator(iter)) {
return new LazyIterator(iter);
}
if (LazyIterator.isIterable(iter)) {
return new LazyIterator(iter[Symbol.iterator]());
}
throw new TypeError('Value given is not iterable or an iterator');
}
// Alias for above.
static for(iter) {
return LazyIterator.from(iter);
}
/**
* Creates a lazy iterator for a sequence of items.
* @param {any[]} items Items to yield.
* @returns {LazyIterator} The iterator.
*/
static of(...items) {
return new LazyIterator(items[Symbol.iterator]());
}
/**
* Generates an iterator yielding a range of integers.
* @param {number} [start=0] The start value.
* @param {number} [end=Infinity] The end value.
* @param {number} [step=1] The step value.
* @param {boolean} [inclusive=false] Whether or not this is an inclusive range.
* @returns {LazyIterator} The iterator.
*/
static range(start = 0, end = Infinity, step = 1, inclusive = false) {
if (inclusive) {
return new LazyIterator(function* range() {
for (let i = start; i <= end; i += step) {
yield i;
}
}());
}
return new LazyIterator(function* range() {
for (let i = start; i < end; i += step) {
yield i;
}
}());
}
/**
* Creates an iterator that yields an item for a certain amount of times.
* @param {any} item Item to yield.
* @param {number} [amount=Infinity] Amount of times to repeat.
* @returns {LazyIterator} The iterator.
*/
static repeat(item, amount = Infinity) {
return new LazyIterator(function* repeat() {
for (let i = 0; i < amount; i++) {
yield item;
}
}());
}
/**
* Creates an iterator that repeatedly yields the result of a function.
* @param {Generating} fn Generating function.
* @param {number} [amount=Infinity] Amount of times to repeat.
* @returns {LazyIterator} The iterator.
*/
static repeatWith(fn, amount = Infinity) {
return new LazyIterator(function* repeatWith() {
for (let i = 0; i < amount; i++) {
yield fn();
}
}());
}
/**
* Creates an iterator where each element is the previous element with the given function applied to it.
* @param {Mapping} fn Mapping function.
* @param {any} init Initial value.
* @returns {LazyIterator} The iterator.
*/
static iterate(fn, init) {
return new LazyIterator(function* iterate() {
let val = init;
while (true) {
yield val;
val = fn(val);
}
}());
}
}
const sameValueZero = (a, b) => {
if (a === 0 && b === 0) {
return 1 / a === 1 / b;
}
if (a === b) {
return true;
}
return isNaN(a) && isNaN(b);
};
const consFunctions = {
Array: () => {
return [];
},
Set: () => {
return new Set();
},
Map: () => {
return new Map();
},
String: () => {
return '';
}
};
const extendFunctions = {
Array: (c, i) => {
c.push(i);
return c;
},
Set: (c, i) => {
c.add(i);
return c;
},
Map: (c, i) => {
c.set(i[0], i[1]);
return c;
},
String: (c, i) => {
return c + i;
}
};
for (const type of ['Array', 'Set', 'Map', 'String']) {
const fnName = `collect${type}`;
Object.defineProperty(LazyIterator.prototype, fnName, {
value: {
// eslint-disable-next-line func-names
[fnName]: function () {
return this.collect(consFunctions[type], extendFunctions[type]);
}
}[fnName],
writable: true,
enumerable: false,
configurable: true
});
}
for (const method of ['partition', 'unzip', 'group', 'categorize']) {
for (const type of ['Array', 'Set', 'Map', 'String']) {
const fnName = `${method}${type}`;
Object.defineProperty(LazyIterator.prototype, fnName, {
value: {
// eslint-disable-next-line func-names
[fnName]: function (a1) {
return this[method](a1, consFunctions[type], extendFunctions[type]);
}
}[fnName],
writable: true,
enumerable: false,
configurable: true
});
}
}
class StepIterator extends LazyIterator {
constructor(iterator, stepSize) {
super(iterator);
this.stepSize = stepSize;
}
next() {
const item = this.iterator.next();
for (let i = 0; i < this.stepSize - 1; i++) {
this.iterator.next();
}
return item;
}
}
class SkipIterator extends LazyIterator {
constructor(iterator, skipAmount) {
super(iterator);
this.skipAmount = skipAmount;
}
next() {
if (this.skipAmount !== 0) {
this.skipAmount--;
this.iterator.next();
return this.next();
}
return this.iterator.next();
}
}
class TakeIterator extends LazyIterator {
constructor(iterator, takeAmount) {
super(iterator);
this.takeAmount = takeAmount;
}
next() {
if (this.takeAmount !== 0) {
this.takeAmount--;
return this.iterator.next();
}
return { done: true };
}
}
class SkipWhileIterator extends LazyIterator {
constructor(iterator, fn) {
super(iterator);
this.fn = fn;
this.finishedSkipping = false;
}
next() {
if (!this.finishedSkipping) {
const item = this.iterator.next();
if (item.done) {
return { done: true };
}
if (!this.fn(item.value)) {
this.finishedSkipping = true;
return { done: false, value: item.value };
}
return this.next();
}
return this.iterator.next();
}
}
class TakeWhileIterator extends LazyIterator {
constructor(iterator, fn) {
super(iterator);
this.fn = fn;
this.finishedTaking = false;
}
next() {
if (!this.finishedTaking) {
const item = this.iterator.next();
if (item.done) {
return { done: true };
}
if (!this.fn(item.value)) {
this.finishedTaking = true;
return { done: true };
}
return { done: false, value: item.value };
}
return { done: true };
}
}
class ChunkIterator extends LazyIterator {
constructor(iterator, chunkSize) {
super(iterator);
this.chunkSize = chunkSize;
}