Skip to content

Instantly share code, notes, and snippets.

@FelixLuciano
Created January 26, 2021 08:53
Show Gist options
  • Save FelixLuciano/0ed35078696d99997ab20fbbf40f71f4 to your computer and use it in GitHub Desktop.
Save FelixLuciano/0ed35078696d99997ab20fbbf40f71f4 to your computer and use it in GitHub Desktop.
Javascript implementation of Python's range
function* range(start, stop, step = 1) {
if (stop === undefined) stop = start, start = 0;
for (let i = start; step > 0 ? i < stop : step < 0 ? i > stop : false; i += step) yield i;
}
// for (let index of range(0, 10, 1)
// Array.from(range(0, 10, 1))
// or
// [...range(0, 10, 1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment