Created
July 18, 2019 21:13
-
-
Save saintsGrad15/afd1d420a22af1cdd3908632c780b374 to your computer and use it in GitHub Desktop.
A generator function that returns an Array consisting of [index, element]. Behaves like the Python function.
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
function* enumerate(array) | |
{ | |
/** | |
* Duplicates the functionality of Python's enumerate function. | |
* | |
* Return, for each element of 'array' an array the element's index and the element. | |
* | |
* e.g. | |
* | |
* [i, elem] | |
* | |
* :param: array: (Array) Any array. | |
* | |
* :yield: For each element of 'array' this generator function will yield an Array of the form | |
* [<element index≥, element] | |
*/ | |
let counter = 0; | |
for (let element of array) | |
yield [counter++, element]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment