Skip to content

Commit 89688a0

Browse files
committed
feat(zip): add zip function
1 parent 4d2f28c commit 89688a0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

index.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import {
4747
take,
4848
takeWhile,
4949
toArray,
50-
unshift
50+
unshift,
51+
zip
5152
} from "./index";
5253

5354
test("tail", async t => {
@@ -375,3 +376,11 @@ test("scan", async t => {
375376
test("scan1", async t => {
376377
t.deepEqual(await toArray(scan1(asyncIterable([1, 2, 3]), (a, e, i) => a + e * i)), [1, 3, 9]);
377378
});
379+
380+
test("zip", async t => {
381+
t.deepEqual(await toArray(zip(asyncIterable([1, 2, 3]), asyncIterable([6, 5, 4, 3, 2, 1]))), [
382+
[1, 6],
383+
[2, 5],
384+
[3, 4]
385+
]);
386+
});

index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,31 @@ export function scan1Fn<T>(
988988
): (iterable: AsyncIterableLike<T>) => AsyncIterable<T> {
989989
return iterable => scan1(iterable, f);
990990
}
991+
992+
export async function* zip<T, U>(
993+
a: AsyncIterableLike<T>,
994+
b: AsyncIterableLike<U>
995+
): AsyncIterable<readonly [T, U]> {
996+
const ait = asyncIterator(a);
997+
const bit = asyncIterator(b);
998+
999+
let ar = await ait.next();
1000+
let br = await bit.next();
1001+
1002+
while (ar.done !== true && br.done !== true) {
1003+
yield [ar.value, br.value];
1004+
1005+
ar = await ait.next();
1006+
br = await bit.next();
1007+
}
1008+
}
1009+
1010+
export const asyncZip = zip;
1011+
1012+
export function zipFn<T, U>(
1013+
b: AsyncIterableLike<U>
1014+
): (a: AsyncIterableLike<T>) => AsyncIterable<readonly [T, U]> {
1015+
return a => zip(a, b);
1016+
}
1017+
1018+
export const asyncZipFn = zipFn;

0 commit comments

Comments
 (0)