Created
February 20, 2018 19:21
-
-
Save mfix22/42bd12d10596157f9e6f984c2a1a290a to your computer and use it in GitHub Desktop.
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 range(s, e) { | |
let start | |
let end | |
if (e == null) { | |
end = s | |
start = 0 | |
} else { | |
start = s | |
end = e | |
} | |
return { | |
[Symbol.iterator]() { | |
return { | |
next: () => { | |
if (start < end) { | |
return { value: start++, done: false }; | |
} else { | |
this.index = start; //If we would like to iterate over this again without forcing manual update of the index | |
return { done: true }; | |
} | |
} | |
} | |
} | |
} | |
} | |
for (const i of range(10)) { | |
console.log(i); | |
} | |
for (const i of range(1, 10)) { | |
console.log(i); | |
} | |
[...range(10)].map(i => console.log(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment