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