Last active
December 23, 2018 11:43
-
-
Save Ayc0/93caeb7195741a497d1e25a111e95325 to your computer and use it in GitHub Desktop.
Python's range
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
for (let x of range(10)) { | |
console.log(x); | |
} | |
for (let x of range(-10, 10)) { | |
console.log(x); | |
} | |
for (let x of range(10, -10, -1)) { | |
console.log(x); | |
} |
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
function* range() { | |
let from = 0; | |
let to = 0; | |
let step = 1; | |
if (arguments.length == 1) { | |
to = arguments[0]; | |
} else if (arguments.length >= 2) { | |
from = arguments[0]; | |
to = arguments[1]; | |
} | |
if (arguments.length >= 3) { | |
step = arguments[2]; | |
} | |
if (step === 0) { | |
return | |
} | |
if (step > 0) { | |
while (from < to) { | |
yield from; | |
from += step; | |
} | |
} else { | |
while (from > to) { | |
yield from; | |
from += step; | |
} | |
} | |
} | |
module.exports = range |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment