Skip to content

Instantly share code, notes, and snippets.

@padolsey
Created June 8, 2019 12:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save padolsey/4c948712e469d7492a346730ebe4ee2b to your computer and use it in GitHub Desktop.
Save padolsey/4c948712e469d7492a346730ebe4ee2b 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