Skip to content

Instantly share code, notes, and snippets.

@hamlim
Forked from padolsey/number_proxy_range.js
Created June 8, 2019 16:52
Show Gist options
  • Save hamlim/4b8c4b9907c0c68b971955735fc10cc9 to your computer and use it in GitHub Desktop.
Save hamlim/4b8c4b9907c0c68b971955735fc10cc9 to your computer and use it in GitHub Desktop.
Number.prototype.to = function*(n) {
let finish = n.valueOf();
let start = this.valueOf();
let finishLessThan = start >= finish;
for (
let i = start;
finishLessThan ? i >= finish: i <= finish;
i += finishLessThan ? -1 : +1
) yield i;
};
Object.setPrototypeOf(Number.prototype, new Proxy({}, {
get: (_, finish, start) => start.to(finish)
}));
// Loop from 20 to 200
for (let n of 20[200]) {}
// Loop from -1000 to 0 (Neg must be wrapped in paren)
for (let n of (-1000)[0]) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment