-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterPlus.ts
3298 lines (3167 loc) · 104 KB
/
IterPlus.ts
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
/* o:import {IterPlus as SyncIterPlus} from "./IterPlus"; */
/* o:import {PromiseOrValue} from "./util"; */
import {CircularBuffer} from "./CircularBuffer";
/**
* Tests if an object is an iterator.
* @param obj The object to test for.
* @returns If `obj` is an iterator.
*/
export function /* r:isAsyncIter */ isIter(
obj: any
): obj is /* o:Async- */ Iterator<unknown> {
return (
typeof obj === "object" &&
obj !== null &&
typeof obj.next === "function"
);
}
/**
* Tests if an object is iterable.
* @param obj The object to test for.
* @returns If `obj` is an iterable.
*/
export function /* r:canAsyncIter */ canIter(
obj: any
): obj is /* o:Async- */ Iterable<unknown> {
return (
/* o://- */ typeof obj === "string" ||
(typeof obj === "object" &&
obj !== null &&
Symbol./* r:asyncIterator */ iterator in obj)
);
}
/**
* Dirty workaround for Prettier moving comments.
*/
type CurIter<T> = /* o:Async- */ Iterator<T>;
/**
* Dirty workaround for Prettier moving comments.
*/
type CurIterable<T> = /* o:Async- */ Iterable<T>;
/**
* Typescript-abusing type that wraps every type in a tuple type with an array.
*/
type ArrayMap<T extends unknown[]> = {
[K in keyof T]: T[K][];
};
/**
* Typescript-abusing type that wraps every type in a tuple type with an Iterable.
*/
type /* o:Async- */ IterableMap<T extends unknown[]> = {
[K in keyof T]: CurIterable<T[K]>;
};
/**
* The value of null to use.
*
* Defaults to `null`.
*/
/* o:// */ export const nullVal = null;
/* o:import {nullVal} from "./IterPlus"; */
/**
* The type of null to use.
*/
/* o://- */ export type Null = typeof nullVal;
/* o:import {Null} from "./IterPlus"; */
/**
* A wrapper around an iterator to add additional functionality. The types intentionally ignore return value.
*
* Many of the methods consume elements of the iterator,
* so use `tee` the iterator into two first if you want to preserve elements.
*
* @typeParam T The item type of the iterator.
*/
export class /* o:Async- */ IterPlus<T>
implements CurIter<T>, /* o:Async- */ Iterable<T>
{
/**
* The internal iterator that this wraps around.
*/
protected internal: /* o:Async- */ Iterator<T>;
/**
* Instantiates an `IterPlus` from any iterator.
*
* @param iter The iterator to wrap around.
*/
constructor(iter: /* o:Async- */ Iterator<T>) {
this.internal = iter;
}
/**
* Yields the next element in the iterator.
*
* @returns The next element.
*/
/* o:async */ next(): /* o:Promise<- */ IteratorResult<T> /* o:-> */ {
return /* o:await */ this.internal.next();
}
/**
* Returns the next value, or null if the iterator ended.
*
* @returns The next value, or null if the iterator ended.
*/
/* o:async */ nextVal(): /* o:Promise<- */ T | Null /* o:-> */ {
const elem = /* o:await */ this.next();
if (elem.done) {
return nullVal;
}
return elem.value;
}
/**
* Makes the iterator work as an iterable.
*
* @returns The same iterator.
*/
[Symbol./* r: asyncIterator */ iterator](): /* o:Async- */ Iterator<T> {
return this;
}
// ==== STATIC METHODS ====
/**
* Generates an empty iterator.
*
* @typeParam T The item yielded by the iterator.
* @returns The generated iterator.
*/
static empty<T>(): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {}
return new /* o:Async- */ IterPlus<T>(ret());
}
/**
* Generates an iterator that yields values from a function and ends once the function returns null.
*
* @typeParam T The item type of the iterator.
* @param func The function to yield values, or null to end the iterator.
* @returns The generated iterator.
*/
static fromFunction<T>(
func: () => /* o:PromiseOrValue<- */ T | Null /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
while (true) {
const val = /* o:await */ func();
if (val === nullVal) {
break;
}
yield val;
}
}
return new /* o:Async- */ IterPlus<T>(ret());
}
/**
* Generates an iterator that lazily yields a single value.
*
* @typeParam T The item type of the iterator.
* @param func The function to generate a single value.
* @returns The generated iterator.
*/
static onceWith<T>(
func: () => /* o:PromiseOrValue<- */ T /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
yield /* o:await */ func();
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that yields a single value.
*
* @typeParam T The item type of the iterator.
* @param val The value to yield.
* @returns The generated iterator.
*/
static once<T>(
val: /* o:PromiseOrValue<- */ T /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
yield /* o:await */ val;
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that endlessly calls a function.
*
* @typeParam T The item type of the iterator.
* @param func The function to generate values.
* @returns The generated iterator.
*/
static repeatWith<T>(
func: () => /* o:PromiseOrValue<- */ T /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
while (true) {
yield /* o:await */ func();
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that endlessly repeats a value.
*
* @typeParam T The item type of the iterator.
* @param val The value to yield.
* @returns The generated iterator.
*/
static repeat<T>(
val: /* o:PromiseOrValue<- */ T /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
while (true) {
yield /* o:await */ val;
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that generates values based on the previous value.
*
* @typeParam T The item type of the iterator.
* @param first The initial value.
* @param func The function to generate new values.
* @returns The generated iterator.
*/
static successors<T>(
first: /* o:PromiseOrValue<- */ T | Null /* o:-> */,
func: (prev: T) => /* o:PromiseOrValue<- */ T | Null /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
let val = /* o:await */ first;
while (true) {
if (val === nullVal) {
break;
}
yield val;
val = /* o:await */ func(val);
}
}
return new /* o:Async- */ IterPlus(ret());
}
static unfold<T, A>(
func: (accum: A) => /* o:PromiseOrValue<- */ [T, A] | Null /* o:-> */,
init: /* o:PromiseOrValue<- */ A /* o:-> */
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
let accum: A = /* o:await */ init;
while (true) {
const pair = /* o:await */ func(accum);
if (pair === nullVal) {
break;
}
yield pair[0];
accum = pair[1];
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that cycles through an iterable.
*
* While this **does** work on infinite iterators,
* it should be avoided as it stores all elements,
* leading to an ever-growing memory usage.
*
* @typeParam T The item type of the iterator.
* @param data The iterable to cycle through.
* @returns The generated iterator.
*/
static cycle<T>(
data: /* o:Async- */ Iterable<T>
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
const cache = [];
/* r:for await */ for (const item of data) {
yield item;
cache.push(item);
}
while (true) {
yield* cache;
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that iterates through lexicographically sorted combinations without repetition of a dataset.
*
* @typeParam T The item type of the iterator.
* @param data The data to generate combinations from.
* @param count The number of elements in each combination.
* @returns The generated iterator.
*/
static combinations<T>(
data: T[],
count: number = data.length
): /* o:Async- */ IterPlus<T[]> {
/* o:async */ function* ret() {
if (count > data.length || count < 0) {
return;
}
const indices: number[] = [];
for (let i = 0; i < count; i++) {
indices.push(i);
}
while (true) {
yield indices.map((v) => data[v]);
let i;
for (i = 0; i < indices.length; i++) {
if (indices[indices.length - i - 1] < data.length - i - 1) {
indices[indices.length - i - 1]++;
break;
}
}
if (i === indices.length) {
break;
}
i--;
while (i >= 0) {
indices[indices.length - i - 1] =
indices[indices.length - i - 2] + 1;
i--;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that iterates through lexicographically sorted combinations with repetition of a dataset.
*
* @typeParam T The item type of the iterator.
* @param data The data to generate combinations from.
* @param count The number of elements in each combination.
* @returns The generated iterator.
*/
static combinationsWithRepetition<T>(
data: T[],
count: number = data.length
): /* o:Async- */ IterPlus<T[]> {
/* o:async */ function* ret() {
if (data.length <= 0 || count < 0) {
return;
}
const indices: number[] = [];
for (let i = 0; i < count; i++) {
indices.push(0);
}
while (true) {
yield indices.map((v) => data[v]);
let i;
for (i = 0; i < indices.length; i++) {
if (indices[indices.length - i - 1] < data.length - 1) {
indices[indices.length - i - 1]++;
break;
}
}
if (i === indices.length) {
break;
}
i--;
while (i >= 0) {
indices[indices.length - i - 1] =
indices[indices.length - i - 2];
i--;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that iterates through lexicographically sorted permutations without repetition of a dataset.
*
* @typeParam T The item type of the iterator.
* @param data The data to generate permutations from.
* @param count The number of elements in each permutations.
* @returns The generated iterator.
*/
static permutations<T>(
data: T[],
count: number = data.length
): /* o:Async- */ IterPlus<T[]> {
/* o:async */ function* ret() {
if (count > data.length || count < 0) {
return;
}
if (count === 0) {
yield [];
return;
}
const indices: number[] = [];
const cycles: number[] = [];
for (let i = 0; i < data.length; i++) {
indices.push(i);
}
for (let i = data.length; i > data.length - count; i--) {
cycles.push(i);
}
yield indices.slice(0, count).map((v) => data[v]);
while (true) {
let i;
for (i = count - 1; i >= 0; i--) {
cycles[i]--;
if (cycles[i] === 0) {
const first = indices[i];
for (let j = i; j < indices.length - 1; j++) {
indices[j] = indices[j + 1];
}
indices[indices.length - 1] = first;
cycles[i] = data.length - i;
} else {
const temp = indices[i];
indices[i] = indices[indices.length - cycles[i]];
indices[indices.length - cycles[i]] = temp;
yield indices.slice(0, count).map((v) => data[v]);
break;
}
}
if (i < 0) {
return;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that iterates through lexicographically sorted permutations with repetition of a dataset.
*
* @typeParam T The item type of the iterator.
* @param data The data to generate permutations from.
* @param count The number of elements in each permutations.
* @returns The generated iterator.
*/
static permutationsWithRepetition<T>(
data: T[],
count: number = data.length
): /* o:Async- */ IterPlus<T[]> {
/* o:async */ function* ret() {
if (data.length <= 0 || count < 0) {
return;
}
if (count === 0) {
yield [];
return;
}
const indices: number[] = [];
for (let i = 0; i < count; i++) {
indices.push(0);
}
while (true) {
yield indices.map((v) => data[v]);
let i;
for (i = 0; i < indices.length; i++) {
if (indices[indices.length - i - 1] < data.length - 1) {
indices[indices.length - i - 1]++;
break;
}
}
if (i === indices.length) {
break;
}
i--;
while (i >= 0) {
indices[indices.length - i - 1] = 0;
i--;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that iterates through the lexicographically sorted powerset of a dataset.
*
* @typeParam T The item type of the iterator.
* @param data The data to get the powerset of.
* @return The generated iterator.
*/
static powerset<T>(data: T[]): /* o:Async- */ IterPlus<T[]> {
/* o:async */ function* ret() {
for (let i = 0; i <= data.length; i++) {
yield* /* o:Async- */ IterPlus.combinations(data, i);
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates an iterator that generates a lexicographically sorted cartesian product.
*
* @typeParam T The item type of the iterator.
* @param data The iterators to take the product of.
* @returns The generated iterator.
*/
static product<T extends unknown[]>(
...data: ArrayMap<T>
): /* o:Async- */ IterPlus<T> {
/* o:async */ function* ret() {
if (data.length <= 0) {
return;
}
const indices: number[] = [];
for (let i = 0; i < data.length; i++) {
if (data[i].length === 0) {
return;
}
indices.push(0);
}
while (true) {
yield indices.map((v, i) => data[i][v]) as T;
let i;
for (i = 0; i < indices.length; i++) {
if (
indices[data.length - i - 1] <
data[data.length - i - 1].length - 1
) {
indices[data.length - i - 1]++;
break;
}
}
if (i === indices.length) {
break;
}
i--;
while (i >= 0) {
indices[data.length - i - 1] = 0;
i--;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Checks if every element in the iterator matches a predicate.
*
* This function is short-circuiting,
* so if any element returns `false`,
* the function immediately returns `false`.
*
* @param pred The predicate function.
* @returns If every element satisfies the predicate.
*/
/* o:async */ every(
pred: (elem: T) => /* o:PromiseOrValue<- */ boolean /* o:-> */
): /* o:Promise<- */ boolean /* o:-> */ {
/* r:for await */ for (const elem of this) {
if (!(/* o:await */ pred(elem))) {
return false;
}
}
return true;
}
/**
* Checks if some element in the iterator matches a predicate.
*
* This function is short-circuiting,
* so if any element returns `true`,
* the function immediately returns `true`.
*
* @param pred The predicate function.
* @returns If some element satisfies the predicate.
*/
/* o:async */ some(
pred: (elem: T) => /* o:PromiseOrValue<- */ boolean /* o:-> */
): /* o:Promise<- */ boolean /* o:-> */ {
/* r:for await */ for (const elem of this) {
if (/* o:await */ pred(elem)) {
return true;
}
}
return false;
}
/**
* Concatenates one or more iterables to this iterator,
* creating an iterator that yields their elements in sequentual order.
*
* @param iters The iterables to chain to this one.
* @returns The generated iterator.
*/
concat(...iters: /* o:Async- */ Iterable<T>[]): /* o:Async- */ IterPlus<T> {
const that = this;
/* o:async */ function* ret() {
for (const iter of [that, ...iters]) {
/* r:for await */ for (const val of iter) {
yield val;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Lexicographically compares this iterator with another using a comparison function.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @typeParam O The type of the other iterator.
* @param other Iterable to compare to.
* @param cmp A function that should return a negative for less than, zero for equal to,
* and positive for greater than.
* @returns -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.
*/
/* o:async */ compareBy<O>(
other: /* o:Async- */ Iterable<O>,
cmp: (first: T, second: O) => /* o:PromiseOrValue<- */ number /* o:-> */
): /* o:Promise<- */ number /* o:-> */ {
const iter = other[Symbol./* r:asyncIterator */ iterator]();
while (true) {
const a = /* o:await */ this.next();
const b = /* o:await */ iter.next();
if (a.done && b.done) {
return 0;
} else if (a.done) {
return -1;
} else if (b.done) {
return 1;
} else {
const diff = /* o:await */ cmp(a.value, b.value);
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
}
}
}
}
/**
* Lexicographically compares this iterator with another using a key.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @typeParam K The type of the key.
* @param other Iterable to compare to.
* @param key Function to generate a key to compare with from an element.
* @returns -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.
*/
/* o:async */ compareWith<K>(
other: /* o:Async- */ Iterable<T>,
key: (elem: T) => /* o:PromiseOrValue<- */ K /* o:-> */
): /* o:Promise<- */ number /* o:-> */ {
return this.compareBy(
other,
/* o:async */ function (a, b) {
const ak = /* o:await */ key(a);
const bk = /* o:await */ key(b);
if (ak < bk) {
return -1;
}
if (ak > bk) {
return 1;
}
return 0;
}
);
}
/**
* Lexicographically compares this iterator with another.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @param other Iterable to compare to.
* @returns -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.
*/
/* o:async */ compare(
other: /* o:Async- */ Iterable<T>
): /* o:Promise<- */ number /* o:-> */ {
return this.compareBy(
other,
/* o:async */ function (a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
);
}
/**
* Collects the items in this iterator into an array.
*
* @returns An array with the items in the iterator.
*/
/* o:async */ collect(): /* o:Promise<- */ T[] /* o:-> */ {
const ret: T[] = [];
/* r:for await */ for (const item of this) {
ret.push(item);
}
return ret;
}
/**
* Calls a specified collector function with this iterator as its only argument.
*
* @param collector The collector to use.
* @returns The return value of the collector.
*/
collectWith<R>(collector: (iter: /* o:Async- */ IterPlus<T>) => R): R {
return collector(this);
}
/**
* Calls a specified constructor with this iterator as its only argument.
*
* @param ctor The constructor to use.
* @returns The constructed value.
*/
construct<R>(ctor: new (item: /* o:Async- */ IterPlus<T>) => R): R {
return new ctor(this);
}
/**
* Counts the number of items in this iterator.
*
* @returns The number of items in the iterator.
*/
/* o:async */ count(): /* o:Promise<- */ number /* o:-> */ {
let ret: number = 0;
/* r:for await */ for (const item of this) {
ret++;
}
return ret;
}
/**
* Generates an iterator that yields a 2 element array with the index and the element.
*
* @returns The generated iterator.
*/
enumerate(): /* o:Async- */ IterPlus<[number, T]> {
const that = this;
/* o:async */ function* ret() {
let count = 0;
/* r:for await */ for (const item of that) {
yield [count, item] as [number, T];
count++;
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Checks if this iterator is equal to another using a comparison function.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @typeParam O The type of the other iterable.
* @param other Iterable to compare to.
* @param cmp A function that checks if elements are equal.
* @returns If the two iterators are equal.
*/
/* o:async */ equalsBy<O>(
other: /* o:Async- */ Iterable<O>,
cmp: (
first: T,
second: O
) => /* o:PromiseOrValue<- */ boolean /* o:-> */
): /* o:Promise<- */ boolean /* o:-> */ {
const iter = other[Symbol./* r:asyncIterator */ iterator]();
while (true) {
const a = /* o:await */ this.next();
const b = /* o:await */ iter.next();
if (a.done && b.done) {
return true;
} else if (a.done || b.done) {
return false;
} else {
const eq = /* o:await */ cmp(a.value, b.value);
if (!eq) {
return false;
}
}
}
}
/**
* Checks if this iterator is equal to another using a key.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @typeParam K The type of the key.
* @param other Iterable to compare to.
* @param key Function to generate a key to compare with from an element.
* @returns If the two iterators are equal.
*/
/* o:async */ equalsWith<K>(
other: /* o:Async- */ Iterable<T>,
key: (elem: T) => /* o:PromiseOrValue<- */ K /* o:-> */
): /* o:Promise<- */ boolean /* o:-> */ {
const iter = other[Symbol./* r:asyncIterator */ iterator]();
while (true) {
const a = /* o:await */ this.next();
const b = /* o:await */ iter.next();
if (a.done && b.done) {
return true;
} else if (a.done || b.done) {
return false;
} else {
const eq =
/* o:await */ key(a.value) === /* o:await */ key(b.value);
if (!eq) {
return false;
}
}
}
}
/**
* Checks if this iterator is equal to another.
*
* This function is short-circuiting,
* so it stops on the first inequality.
*
* @param other Iterable to compare to.
* @returns If the two iterators are equal.
*/
/* o:async */ equals(
other: /* o:Async- */ Iterable<T>
): /* o:Promise<- */ boolean /* o:-> */ {
const iter = other[Symbol./* r:asyncIterator */ iterator]();
while (true) {
const a = /* o:await */ this.next();
const b = /* o:await */ iter.next();
if (a.done && b.done) {
return true;
} else if (a.done || b.done) {
return false;
} else {
const eq = a.value === b.value;
if (!eq) {
return false;
}
}
}
}
/**
* Generates an iterator that only yields elements that match a predicate.
*
* @param pred The predicate function.
* @returns The generated iterator.
*/
filter(
pred: (elem: T) => /* o:PromiseOrValue<- */ boolean /* o:-> */
): /* o:Async- */ IterPlus<T> {
const that = this;
/* o:async */ function* ret() {
/* r:for await */ for (const elem of that) {
if (/* o:await */ pred(elem)) {
yield elem;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Generates a mapped iterator that yields non-null elements.
*
* @typeParam K The resulting type.
* @typeParam N The type of the null value.
* @param func The mapping function.
* @returns The generated iterator.
*/
filterMap<K>(
func: (elem: T) => /* o:PromiseOrValue<- */ K | Null /* o:-> */
): /* o:Async- */ IterPlus<K> {
const that = this;
/* o:async */ function* ret() {
/* r:for await */ for (const elem of that) {
const val = /* o:await */ func(elem);
if (val !== nullVal) {
yield val as K;
}
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Finds an element that satisfies a predicate.
*
* This function is short-circuiting,
* so it stops on the first match.
*
* @param pred The predicate function.
* @returns The element, or null if none was found.
*/
/* o:async */ find(
pred: (elem: T) => /* o:PromiseOrValue<- */ boolean /* o:-> */
): /* o:Promise<- */ T | Null /* o:-> */ {
/* r:for await */ for (const elem of this) {
if (/* o:await */ pred(elem)) {
return elem;
}
}
return nullVal;
}
/**
* Runs a function on every element and returns the first non-null element.
*
* This function is short-circuiting,
* so it stops on the first match.
*
* @typeParam K The resulting type.
* @typeParam N The type of the null value.
* @param func The mapping function.
* @returns The element, or null if none was found.
*/
/* o:async */ findMap<K>(
func: (elem: T) => /* o:PromiseOrValue<- */ K | Null /* o:-> */
): /* o:Promise<- */ K | Null /* o:-> */ {
/* r:for await */ for (const elem of this) {
const val = /* o:await */ func(elem);
if (val !== nullVal) {
return val as K;
}
}
return nullVal;
}
/**
* Flattens an iterator of iterables,
* yielding an iterator that sequentially produces their elements.
*
* @typeParam K The internal type.
* @returns The generated iterator.
*/
flatten<K>(
this: /* o:Async- */ IterPlus</* o:Iterable<K> | Async- */ Iterable<K>>
): /* o:Async- */ IterPlus<K> {
const that = this;
/* o:async */ function* ret() {
/* r:for await */ for (const iterable of that) {
yield* iterable;
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Lazily maps an iterator, creating a new iterator where each element has been modified by a function.
*
* If you want to immediately run a function on all elements of the iterator, use `forEach` instead.
*
* @typeParam K The resulting type.
* @param func The mapping function.
* @returns The generated iterator.
*/
map<K>(
func: (elem: T) => /* o:PromiseOrValue<- */ K /* o:-> */
): /* o:Async- */ IterPlus<K> {
const that = this;
/* o:async */ function* ret() {
/* r:for await */ for (const elem of that) {
yield /* o:await */ func(elem);
}
}
return new /* o:Async- */ IterPlus(ret());
}
/**
* Maps an iterator of iterables,
* and calls a function with the contents of the iterable as the argument.
*
* @typeParam K The iterable type.
* @typeParam R The resulting type.
* @param func The mapping function.
* @returns The generated iterator.
*/