Last active
February 20, 2017 16:40
-
-
Save yefremov/2ca3ebbfe30dafbf00254ba5a94e8ddd to your computer and use it in GitHub Desktop.
Two versions of a collection iterator factory
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 iterate(iterable) { | |
var index = -1; | |
return function next() { | |
return ++index < iterable.length ? iterable[index] : null; | |
} | |
}; | |
function makeIterator(iterable) { | |
var index = -1; | |
return function next() { | |
return ++index < iterable.length | |
? { value: iterable[index], key: index, next: next, done: false } | |
: { done: true }; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment