Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Created February 7, 2025 03:52
Show Gist options
  • Save xantiagoma/7c7e67dcdabcbd947c578a37e14ac603 to your computer and use it in GitHub Desktop.
Save xantiagoma/7c7e67dcdabcbd947c578a37e14ac603 to your computer and use it in GitHub Desktop.
export function* enumerate<T>(
iterable: Iterable<T>,
start = 0,
): Generator<[number, T]> {
let index = start;
for (const value of iterable) {
yield [index, value];
index++;
}
}
export async function* asyncEnumerate<T>(
iterable: AsyncIterable<T> | Iterable<T>,
start = 0,
): AsyncGenerator<[number, T]> {
let index = start;
for await (const value of iterable) {
yield [index, value];
index++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment