Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan
Created July 11, 2017 20:13
Show Gist options
  • Save PaulKinlan/df3cd68e4040186bac651bcecab61640 to your computer and use it in GitHub Desktop.
Save PaulKinlan/df3cd68e4040186bac651bcecab61640 to your computer and use it in GitHub Desktop.
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