Created
June 8, 2019 12:01
-
-
Save padolsey/4c948712e469d7492a346730ebe4ee2b to your computer and use it in GitHub Desktop.
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
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