Created
August 1, 2016 11:57
-
-
Save jannesaranpaa/5b97130f21d8c4dacb9111d2a41da7ea to your computer and use it in GitHub Desktop.
JavaScript [ES6] implementation of pythons' range()
This file contains hidden or 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
| function* range(start, stop, step = 1) | |
| { | |
| if (stop <= start & step > 0 | stop >= start & step < 0) | |
| { | |
| throw "Stop value is out of range"; | |
| } | |
| else if (start >= stop & step > 0 | start <= stop & step < 0) | |
| { | |
| throw "Start value is out of range"; | |
| } | |
| else | |
| { | |
| for (let i = start; i < stop & step > 0 | i > stop & step < 0; i += step) | |
| { | |
| yield(i); | |
| } | |
| } | |
| } | |
| for (let i of range(20, 0, -2)) | |
| { | |
| console.log(i); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment