Skip to content

Instantly share code, notes, and snippets.

@mhingston
Created December 26, 2016 22:19
Show Gist options
  • Save mhingston/488cd5391e17095290e96bbdd8f385a4 to your computer and use it in GitHub Desktop.
Save mhingston/488cd5391e17095290e96bbdd8f385a4 to your computer and use it in GitHub Desktop.
Python's xrange() for javascript
function* xrange(start, stop, step)
{
if(stop === undefined)
{
stop = start;
start = 0;
}
if(step === undefined)
{
step = 1;
}
for(let i=start; start < stop ? i < stop : i > stop; i += step)
{
yield i;
}
}
// Usage example:
//for (const i of xrange(10))
//{
// console.log(i);
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment