Created
September 20, 2020 07:13
-
-
Save tripulse/96f4dd6508a2254d040ab1ea43ce78db to your computer and use it in GitHub Desktop.
Python-like range implementation, using JS generators for lazy evaulation.
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(...args) { | |
var start = 0; | |
var stop = 0; | |
var step = 1; | |
if(args.length == 1) | |
stop = args[0]; | |
else if(args.length == 2) { | |
start = args[0]; | |
stop = args[1]; | |
} | |
else if(args.length != 0) { | |
start = args[0]; | |
stop = args[1]; | |
step = args[2]; | |
} | |
while(start < stop) { | |
yield start; | |
start += step; | |
} | |
yield stop; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment