Created
February 9, 2021 02:16
-
-
Save bishil06/f887012106273f51d89b019832df05f0 to your computer and use it in GitHub Desktop.
_JavaScript_enumerate_iterable
This file contains hidden or 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 *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