Created
April 15, 2023 04:40
-
-
Save AlexXanderGrib/9952ed984462dfb9b25407e358464d5d to your computer and use it in GitHub Desktop.
Python's zip in TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* zip<T extends readonly unknown[]>( | |
...iterables: { [key in keyof T]: Iterable<T[key]> } | |
): Generator<T, void, void> { | |
const iterators = iterables.map((iterable) => iterable[Symbol.iterator]()); | |
outer: while (iterators.length > 0) { | |
const values: unknown[] = []; | |
for (const iterator of iterators) { | |
const result = iterator.next(); | |
if (result.done) { | |
break outer; | |
} | |
values.push(result.value); | |
} | |
yield values as unknown as T; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment