Created
February 7, 2025 03:52
-
-
Save xantiagoma/7c7e67dcdabcbd947c578a37e14ac603 to your computer and use it in GitHub Desktop.
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
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