Skip to content

Instantly share code, notes, and snippets.

@taskie
Created May 24, 2015 02:39
Show Gist options
  • Save taskie/7c48ea8cadec822f4195 to your computer and use it in GitHub Desktop.
Save taskie/7c48ea8cadec822f4195 to your computer and use it in GitHub Desktop.
generator "range" like Python 3
function * range(n, end, step = 1) {
if (typeof end === "undefined") {
end = n;
n = 0;
}
if (step === 0) {
throw Error("step must not be zero")
}
if (end > n && step > 0) {
for (let i = n; i < end; i += step) yield i;
} else if (end < n && step < 0) {
for (let i = n; i > end; i += step) yield i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment