Skip to content

Instantly share code, notes, and snippets.

@mfix22
Created February 20, 2018 19:21
Show Gist options
  • Save mfix22/42bd12d10596157f9e6f984c2a1a290a to your computer and use it in GitHub Desktop.
Save mfix22/42bd12d10596157f9e6f984c2a1a290a to your computer and use it in GitHub Desktop.
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