Last active
August 29, 2015 13:58
-
-
Save mstade/10012898 to your computer and use it in GitHub Desktop.
ES6 rewindable iterators
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 range(n) { | |
var i = 0 | |
return { next: step, rewind: rewind } | |
function step() { | |
return i++ < n? { value: i } : { done: '110%' } | |
} | |
function rewind() { i = 0 } | |
} | |
var ten = range(5) | |
for (var n of ten) console.log(n) | |
for (var n of ten) console.log('not called') | |
ten.rewind() | |
console.log('erase and rewind') | |
for (var n of ten) console.log(n) // Works again |
Author
mstade
commented
Apr 7, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment