Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan PaulKinlan/range.js
Created Jul 11, 2017

Embed
What would you like to do?
range.js
const range = (stop) => { stop = stop || 0; const shouldStop = (n) => stop >= 0 ? (n < stop) : (n > stop); const interval = (n) => stop >= 0 ? n + 1 : n - 1; let itr = {}; itr[Symbol.iterator] = function* () { let i = 0; while(shouldStop(i)) { yield i; i = interval(i);}}; return itr; };
for(let i of range(100))
console.log(i)
for(let i of range(-100))
console.log(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.