Skip to content

Instantly share code, notes, and snippets.

@AlexXanderGrib
Created April 15, 2023 04:40
Show Gist options
  • Save AlexXanderGrib/9952ed984462dfb9b25407e358464d5d to your computer and use it in GitHub Desktop.
Save AlexXanderGrib/9952ed984462dfb9b25407e358464d5d to your computer and use it in GitHub Desktop.
Python's zip in TS
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