Skip to content

Instantly share code, notes, and snippets.

@bishil06
Created February 9, 2021 02:16
Show Gist options
  • Save bishil06/f887012106273f51d89b019832df05f0 to your computer and use it in GitHub Desktop.
Save bishil06/f887012106273f51d89b019832df05f0 to your computer and use it in GitHub Desktop.
_JavaScript_enumerate_iterable
function *enumerate(init=0, iter) {
if (init[Symbol.iterator]) {
iter = init[Symbol.iterator]();
init = 0;
}
let count = init;
for(const a of iter) {
yield [count, a];
count += 1;
}
}
console.log(...enumerate([10, 20, 30])); // [ 0, 10 ] [ 1, 20 ] [ 2, 30 ]
console.log(...enumerate(5, [10, 20, 30])); // [ 5, 10 ] [ 6, 20 ] [ 7, 30 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment