-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-iter.ts
792 lines (706 loc) · 21.7 KB
/
async-iter.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
import { asyncTee, asyncTeeN, forAwaitableIterator } from './common';
import { ForOfAwaitable } from './common';
// OPERATORS
export function map<A, B>(f: (x: A) => B) {
return async function*(xs: ForOfAwaitable<A>): AsyncIterableIterator<B> {
for await (const x of xs) yield f(x);
};
}
export function tap<X>(f: (x: X) => any) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
for await (const x of xs) {
f(x);
yield x;
}
};
}
export const inspect = tap;
export function forEach<X>(f: (x: X) => any) {
return async function(xs: ForOfAwaitable<X>): Promise<void> {
for await (const x of xs) f(x);
};
}
export const subscribe = forEach;
export function reduce<X, R>(f: (acc: R, x: X) => R, init: R) {
return async function(xs: ForOfAwaitable<X>): Promise<R> {
let res = init;
for await (const x of xs) {
res = f(res, x);
}
return res;
};
}
export function scan<X, R>(f: (acc: R, x: X) => R, init: R) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<R> {
let res = init;
for await (const x of xs) {
res = f(res, x);
yield res;
}
};
}
export const reducutions = scan;
export function some<X>(p: (x: X) => boolean) {
return async function(xs: ForOfAwaitable<X>): Promise<boolean> {
for await (const x of xs) {
if (p(x)) return true;
}
return false;
};
}
export function every<X>(p: (x: X) => boolean) {
return async function(xs: ForOfAwaitable<X>): Promise<boolean> {
for await (const x of xs) {
if (!p(x)) return false;
}
return true;
};
}
export function filter<X>(p: (x: X) => boolean) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
for await (const x of xs) {
if (p(x)) yield x;
}
};
}
export function partition<X>(p: (x: X) => boolean) {
return function(xs: ForOfAwaitable<X>): [AsyncIterableIterator<X>, AsyncIterableIterator<X>] {
const [xs1, xs2] = asyncTee(xs);
return [filter(p)(xs1), filter((x: X) => !p(x))(xs2)];
};
}
export function skip<X>(n: number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
let i = 0;
for await (const x of xs) {
if (++i <= n) continue;
yield x;
}
};
}
export function take<X>(n: number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
let i = 0;
for await (const x of xs) {
if (++i > n) break;
yield x;
}
};
}
// TODO: rename?
export function partitionAt<X>(n: number) {
return function(xs: ForOfAwaitable<X>): [AsyncIterableIterator<X>, AsyncIterableIterator<X>] {
const [xs1, xs2] = asyncTee(xs);
return [take<X>(n)(xs1), skip<X>(n)(xs2)];
};
}
export const splitAt = partitionAt;
export function skipWhile<X>(f: (x: X) => boolean) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
const it = forAwaitableIterator(xs);
let first: X;
for await (const x of it) {
first = x;
if (!f(x)) break;
}
yield first;
for await (const x of it) yield x;
};
}
export function takeWhile<X>(f: (x: X) => boolean) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
for await (const x of xs) {
if (f(x)) yield x;
else break;
}
};
}
export function partitionWhile<X>(f: (x: X) => boolean) {
return function(xs: ForOfAwaitable<X>): [AsyncIterableIterator<X>, AsyncIterableIterator<X>] {
const [xs1, xs2] = asyncTee(xs);
return [takeWhile<X>(f)(xs1), skipWhile<X>(f)(xs2)];
};
}
export function find<X>(p: (x: X) => boolean) {
return async function(xs: ForOfAwaitable<X>): Promise<X | null> {
for await (const x of xs) {
if (p(x)) return x;
}
return null;
};
}
export function findIndex<X>(p: (x: X) => boolean) {
return async function(xs: ForOfAwaitable<X>): Promise<number> {
let i = 0;
for await (const x of xs) {
if (p(x)) return i;
i++;
}
return -1;
};
}
export function pluck<X>(key: string | number) {
return async function*(xs: ForOfAwaitable<Object>): AsyncIterableIterator<X | null> {
for await (const x of xs) yield x[key];
};
}
// like pluck, but accepts an iterable of keys
export function select<X>(keys: Array<string | number>) {
return async function*(xs: ForOfAwaitable<Object>): AsyncIterableIterator<X | null> {
for await (const x of xs) {
let r = x;
for (const k of keys) {
r = r != null ? r[k] : undefined;
}
yield r as (X | null);
}
};
}
export function unzip2<X, Y>() {
return function(xs: ForOfAwaitable<[X, Y]>): [AsyncIterableIterator<X>, AsyncIterableIterator<Y>] {
const [xs1, xs2] = asyncTee(xs);
return [pluck<X>(0)(xs1), pluck<Y>(1)(xs2)];
};
}
export function unzip3<X, Y, Z>() {
return function(
xs: ForOfAwaitable<[X, Y, Z]>,
): [AsyncIterableIterator<X>, AsyncIterableIterator<Y>, AsyncIterableIterator<Z>] {
const [xs1, xs2, xs3] = asyncTeeN(xs, 3);
return [pluck<X>(0)(xs1), pluck<Y>(1)(xs2), pluck<Z>(2)(xs3)];
};
}
export function unzip(n: number = 2) {
return function(xs: ForOfAwaitable<{}[]>): AsyncIterableIterator<{}>[] {
const xss = asyncTeeN(xs, n);
return xss.map((xs, i) => pluck(i)(xs));
};
}
export function groupBy<X, K>(f: (x: X) => K) {
return async function(xs: ForOfAwaitable<X>): Promise<Map<K, X[]>> {
const res = new Map<K, X[]>();
for await (const x of xs) {
const key = f(x);
if (!res.has(key)) res.set(key, []);
res.get(key).push(x);
}
return res;
};
}
export function groupByKey<X>(key: string | number) {
return groupBy<X, string | number>((x: X) => x[key]);
}
export function mapKeys<KA, KB, V>(f: (k: KA) => KB) {
return async function*(xs: ForOfAwaitable<[KA, V]>): AsyncIterableIterator<[KB, V]> {
for await (const [k, v] of xs) yield [f(k), v];
};
}
export function mapValues<VA, VB, K>(f: (v: VA) => VB) {
return async function*(xs: ForOfAwaitable<[K, VA]>): AsyncIterableIterator<[K, VB]> {
for await (const [k, v] of xs) yield [k, f(v)];
};
}
export function pairwise<X>() {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X]> {
const it = forAwaitableIterator(xs);
let prev = (await it.next()).value;
for await (const x of it) {
yield [prev, x];
prev = x;
}
};
}
export function length() {
return async function(xs: ForOfAwaitable<{}>): Promise<number> {
let c = 0;
for await (const _ of xs) c++;
return c;
};
}
export function min() {
return async function(xs: ForOfAwaitable<number>): Promise<number> {
let res = Number.POSITIVE_INFINITY;
for await (const x of xs) {
if (x < res) res = x;
}
return res;
};
}
export function max() {
return async function(xs: ForOfAwaitable<number>): Promise<number> {
let res = Number.NEGATIVE_INFINITY;
for await (const x of xs) {
if (x > res) res = x;
}
return res;
};
}
export function minMax() {
return async function(xs: ForOfAwaitable<number>): Promise<[number, number]> {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
for await (const x of xs) {
if (x < min) min = x;
if (x > max) max = x;
}
return [min, max];
};
}
export function minBy<X>(cf: (a: X, b: X) => number) {
return async function(xs: ForOfAwaitable<X>): Promise<X | null> {
const it = forAwaitableIterator(xs);
const { done, value } = await it.next();
if (done) return null;
let res = value;
for await (const x of it) if (cf(x, res) < 0) res = x;
return res;
};
}
export function maxBy<X>(cf: (a: X, b: X) => number) {
return async function(xs: ForOfAwaitable<X>): Promise<X | null> {
const it = forAwaitableIterator(xs);
const { done, value } = await it.next();
if (done) return null;
let res = value;
for await (const x of it) if (cf(x, res) > 0) res = x;
return res;
};
}
export function minMaxBy<X>(cf: (a: X, b: X) => number) {
return async function(xs: ForOfAwaitable<X>): Promise<[X | null, X | null]> {
const it = forAwaitableIterator(xs);
const { done, value } = await it.next();
if (done) return [null, null];
let min = value;
let max = value;
for await (const x of it) {
if (cf(x, min) < 0) min = x;
if (cf(x, max) > 0) max = x;
}
return [min, max];
};
}
export function minByScan<X>(cf: (a: X, b: X) => number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X | null> {
const it = forAwaitableIterator(xs);
const { done, value } = await it.next();
if (done) yield null;
let res = value;
for await (const x of it) {
if (cf(x, res) < 0) res = x;
yield res;
}
};
}
export function maxByScan<X>(cf: (a: X, b: X) => number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X | null> {
const it = forAwaitableIterator(xs);
const { done, value } = await it.next();
if (done) yield null;
let res = value;
for await (const x of it) {
if (cf(x, res) > 0) res = x;
yield res;
}
};
}
export function sum(zero: number = 0) {
return async function(xs: ForOfAwaitable<number>): Promise<number> {
let res = zero;
for await (const x of xs) res += x;
return res;
};
}
export function replaceWhen<X, Y>(pf: (x: X) => boolean, ys: ForOfAwaitable<Y>) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X | Y> {
for await (const [x, y] of zip2(xs, ys)) {
if (!pf(x)) yield x;
else yield y;
}
};
}
export function grouped<X>(n: number, step: number = n) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X[]> {
let group = [];
for await (const x of xs) {
group.push(x);
if (group.length === n) {
yield [...group];
for (let i = 0; i < step; i++) group.shift();
}
}
};
}
export function startWith<X>(...as: X[]) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
for await (const a of as) yield a;
for await (const x of xs) yield x;
};
}
export function endWith<X>(...zs: X[]) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
for await (const x of xs) yield x;
for await (const z of zs) yield z;
};
}
export function sort<X>(cf: (a: X, b: X) => number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
let arr = [];
for await (const x of xs) arr.push(x);
for (const x of arr.sort(cf)) yield x;
};
}
export function sortScan<X>(cf: (a: X, b: X) => number) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X[]> {
let arr = [];
for await (const x of xs) {
arr.push(x);
yield [...arr.sort(cf)];
}
};
}
export function flatMap<A, B>(f: (x: A) => B) {
return async function*(xss: ForOfAwaitable<ForOfAwaitable<A>>): AsyncIterableIterator<B> {
for await (const xs of xss) for await (const x of xs) yield await f(x);
};
}
export function distinctUntilChanged<X>(comp: (a: X, b: X) => boolean = (a, b) => a === b) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
const it = forAwaitableIterator(xs);
let { done, value: initial } = await it.next();
if (done) return;
yield initial;
for await (const x of it) if (!comp(x, initial)) yield (initial = x);
};
}
export function unique<X>(comp: (a: X, b: X) => boolean = (a, b) => a === b) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
const arr = [];
for await (const x of xs) arr.push(x);
const unq = arr.filter((x, i, self) => self.findIndex(y => comp(x, y)) === i);
for (const u of unq) yield u;
};
}
export function uniqueSorted<X>(comp: (a: X, b: X) => boolean = (a, b) => a === b) {
return async function*(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
const arr = [];
for await (const x of xs) arr.push(x);
arr.sort();
for await (const x of distinctUntilChanged(comp)(arr)) yield x;
};
}
// CONSTRUCTORS
export async function* range(start = 0, end = Number.MAX_SAFE_INTEGER, step = 1): AsyncIterableIterator<number> {
for (let i = start; end > start ? i < end : i > end; i += step) yield i;
}
// TODO: rename to `entries`?
export async function* enumerate<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[number, X]> {
let i = 0;
for await (const x of xs) yield [i++, x];
}
export async function* concat<X>(...xss: ForOfAwaitable<X>[]): AsyncIterableIterator<X> {
for (const xs of xss) for await (const x of xs) yield x;
}
export const chain = concat;
export async function* zip2<X, Y>(xs: ForOfAwaitable<X>, ys: ForOfAwaitable<Y>): AsyncIterableIterator<[X, Y]> {
const xit = forAwaitableIterator(xs);
const yit = forAwaitableIterator(ys);
while (true) {
const [xr, yr] = await Promise.all([xit.next(), yit.next()]);
if (xr.done || yr.done) break;
yield [xr.value, yr.value];
}
}
export async function* zip3<X, Y, Z>(
xs: ForOfAwaitable<X>,
ys: ForOfAwaitable<Y>,
zs: ForOfAwaitable<Z>,
): AsyncIterableIterator<[X, Y, Z]> {
const xit = forAwaitableIterator(xs);
const yit = forAwaitableIterator(ys);
const zit = forAwaitableIterator(zs);
while (true) {
const [xr, yr, zr] = await Promise.all([xit.next(), yit.next(), zit.next()]);
if (xr.done || yr.done || zr.done) break;
yield [xr.value, yr.value, zr.value];
}
}
export async function* zip(...xss: ForOfAwaitable<{}>[]): AsyncIterableIterator<{}[]> {
const its = xss.map(forAwaitableIterator);
while (true) {
const rs = await Promise.all(its.map(it => it.next()));
if (rs.some(r => r.done)) break;
yield rs.map(r => r.value);
}
}
// TODO: rename? Is this how regular zip commonly works?
export async function* zipOuter(...xss: ForOfAwaitable<{}>[]): AsyncIterableIterator<{}[]> {
const its = xss.map(forAwaitableIterator);
while (true) {
const rs = await Promise.all(its.map(it => it.next()));
if (rs.every(r => r.done)) break;
yield rs.map(r => r.value);
}
}
export async function* product2<A, B>(as: ForOfAwaitable<A>, bs: ForOfAwaitable<B>): AsyncIterableIterator<[A, B]> {
// if (as === bs) [as, bs] = asyncTee(as);
let bs2: ForOfAwaitable<B>;
for await (const a of as) {
[bs, bs2] = asyncTee(bs);
for await (const b of bs2) {
yield [a, b];
}
}
}
export async function* product3<A, B, C>(
as: ForOfAwaitable<A>,
bs: ForOfAwaitable<B>,
cs: ForOfAwaitable<C>,
): AsyncIterableIterator<[A, B, C]> {
// if (as === bs) [as, bs] = asyncTee(as);
// if (as === cs) [as, cs] = asyncTee(as);
// if (bs === cs) [bs, cs] = asyncTee(bs);
let bs2: ForOfAwaitable<B>;
let cs2: ForOfAwaitable<C>;
for await (const a of as) {
[bs, bs2] = asyncTee(bs);
for await (const b of bs2) {
[cs, cs2] = asyncTee(cs);
for await (const c of cs2) {
yield [a, b, c];
}
}
}
}
// TODO: generalize to n parameters
export async function* product(xs: ForOfAwaitable<{}>, ...xss: ForOfAwaitable<{}>[]): AsyncIterableIterator<{}[]> {
throw new Error('Not implemented');
}
// TODO: other name (look at python itertools?)
// TODO: fix implementation
export async function* combinations2<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X]> {
let [as, bs] = asyncTee(xs);
let bs2: ForOfAwaitable<X>;
let i = 1;
for await (const a of as) {
[bs, bs2] = asyncTee(bs);
for await (const b of skip<X>(i++)(bs2)) {
yield [a, b];
}
}
}
export async function* combinations3<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X, X]> {
throw Error('Not implemented');
// let [as, bs, cs] = asyncTeeN(xs, 3);
// let bs2: ForOfAwaitable<X>;
// let cs2: ForOfAwaitable<X>;
// let i = 1;
// let j = 2;
// for await (const a of as) {
// [bs, bs2] = asyncTee(bs);
// for await (const b of skip<X>(i++)(bs2)) {
// [cs, cs2] = asyncTee(cs);
// for await (const c of skip<X>(j++)(cs2)) {
// yield [a, b, c];
// }
// }
// }
}
export async function* combinations(xs: ForOfAwaitable<{}>, r: number = 2): AsyncIterableIterator<{}[]> {
throw Error('Not implemented');
}
export async function* combinationsWithReplacement2<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X]> {
let [as, bs] = asyncTee(xs);
let bs2: ForOfAwaitable<X>;
let i = 0;
for await (const a of as) {
[bs, bs2] = asyncTee(bs);
for await (const b of skip<X>(i++)(bs2)) {
yield [a, b];
}
}
}
export async function* combinationsWithReplacement3<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X, X]> {
throw Error('Not implemented');
// let [as, bs, cs] = asyncTeeN(xs, 3);
// let bs2: ForOfAwaitable<X>;
// let cs2: ForOfAwaitable<X>;
// let i = 0;
// let j = 0;
// for await (const a of as) {
// [bs, bs2] = asyncTee(bs);
// for await (const b of skip<X>(i++)(bs2)) {
// [cs, cs2] = asyncTee(cs);
// for await (const c of skip<X>(j++)(cs2)) {
// yield [a, b, c];
// }
// }
// }
}
export async function* combinationsWithReplacement(xs: ForOfAwaitable<{}>, r: number = 2): AsyncIterableIterator<{}[]> {
throw Error('Not implemented');
}
export async function* permutations2<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X]> {
throw Error('Not implemented');
}
export async function* permutations3<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<[X, X, X]> {
throw Error('Not implemented');
}
export async function* permutations(xs: ForOfAwaitable<{}>, r: number = 2): AsyncIterableIterator<{}[]> {
throw Error('Not implemented');
}
export async function* constantly<X>(value: X): AsyncIterableIterator<X> {
while (true) yield value;
}
export async function* cycle<X>(xs: ForOfAwaitable<X>): AsyncIterableIterator<X> {
let xs2: ForOfAwaitable<X>;
while (true) {
[xs, xs2] = asyncTee(xs);
for await (const x of xs2) yield x;
}
}
export async function* repeat<X>(xs: ForOfAwaitable<X>, n: number): AsyncIterableIterator<X> {
let xs2: ForOfAwaitable<X>;
for (let i = 0; i < n; i++) {
[xs, xs2] = asyncTee(xs);
for await (const x of xs2) yield x;
}
}
export async function* interleave2<X, Y>(xs: ForOfAwaitable<X>, ys: ForOfAwaitable<Y>): AsyncIterableIterator<X | Y> {
const itx = forAwaitableIterator(xs);
const ity = forAwaitableIterator(ys);
while (true) {
const rx = await itx.next();
if (rx.done) break;
else yield rx.value;
const ry = await ity.next();
if (ry.done) break;
else yield ry.value;
}
}
export async function* interleave3<X, Y, Z>(
xs: ForOfAwaitable<X>,
ys: ForOfAwaitable<Y>,
zs: ForOfAwaitable<Z>,
): AsyncIterableIterator<X | Y | Z> {
const itx = forAwaitableIterator(xs);
const ity = forAwaitableIterator(ys);
const itz = forAwaitableIterator(zs);
while (true) {
const rx = await itx.next();
if (rx.done) break;
else yield rx.value;
const ry = await ity.next();
if (ry.done) break;
else yield ry.value;
const rz = await itz.next();
if (rz.done) break;
else yield rz.value;
}
}
export async function* interleave(...xss: ForOfAwaitable<{}>[]): AsyncIterableIterator<{}> {
const its = xss.map(forAwaitableIterator);
// Throwback to the 90s
outerloop: while (true) {
for (const it of its) {
const { done, value } = await it.next();
// Yup, this just happened
if (done) break outerloop;
else yield value;
}
}
}
// https://github.com/Microsoft/TypeScript/issues/17718#issuecomment-402931751
export async function pipe<T1>(x: T1): Promise<T1>;
export async function pipe<T1, T2>(x: T1, f1: (x: T1) => T2): Promise<T2>;
export async function pipe<T1, T2, T3>(x: T1, f1: (x: T1) => T2, f2: (x: T2) => T3): Promise<T3>;
export async function pipe<T1, T2, T3, T4>(x: T1, f1: (x: T1) => T2, f2: (x: T2) => T3, f3: (x: T3) => T4): Promise<T4>;
export async function pipe<T1, T2, T3, T4, T5>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
): Promise<T5>;
export async function pipe<T1, T2, T3, T4, T5, T6>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
): Promise<T6>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
): Promise<T7>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7, T8>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
f7: (x: T7) => T8,
): Promise<T8>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
f7: (x: T7) => T8,
f8: (x: T8) => T9,
): Promise<T9>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
f7: (x: T7) => T8,
f8: (x: T8) => T9,
f9: (x: T9) => T10,
): Promise<T10>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
f7: (x: T7) => T8,
f8: (x: T8) => T9,
f9: (x: T9) => T10,
f10: (x: T10) => T11,
): Promise<T11>;
export async function pipe<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(
x: T1,
f1: (x: T1) => T2,
f2: (x: T2) => T3,
f3: (x: T3) => T4,
f4: (x: T4) => T5,
f5: (x: T5) => T6,
f6: (x: T6) => T7,
f7: (x: T7) => T8,
f8: (x: T8) => T9,
f9: (x: T9) => T10,
f10: (x: T10) => T11,
f11: (x: T11) => T12,
): Promise<T12>;
export async function pipe(x: any, ...fs: Function[]): Promise<any> {
let res = await x;
for (const f of fs) res = await f(res);
return res;
}