Last active
November 27, 2015 18:51
-
-
Save JoshCheek/1537bb4ea2a9d5d5298a to your computer and use it in GitHub Desktop.
Wrote an iterator with Steve Kinney at a JS meetup in Denver (following along with the material at http://thedrearlight.com/bwdid-iterables/#/)
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
let MyIterator = function(n) { | |
this.n = n; | |
} | |
MyIterator.prototype[Symbol.iterator] = function() { | |
var i = -1; | |
var n = this.n; | |
var done = false; | |
return { | |
next: (something) => { | |
if(done) { | |
return {next: undefined, done: done} | |
} else { | |
done = ++i == n; | |
return {value: i, done: done}; | |
} | |
}, | |
throw: (something)=> { /* idk */ }, | |
return: (something)=> { /* idk */ }, | |
} | |
} | |
let iter = new MyIterator(4); | |
for(let val of iter) { | |
console.log(val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment