Skip to content

Instantly share code, notes, and snippets.

@tripulse
Created September 20, 2020 07:13
Show Gist options
  • Save tripulse/96f4dd6508a2254d040ab1ea43ce78db to your computer and use it in GitHub Desktop.
Save tripulse/96f4dd6508a2254d040ab1ea43ce78db to your computer and use it in GitHub Desktop.
Python-like range implementation, using JS generators for lazy evaulation.
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