Skip to content

Instantly share code, notes, and snippets.

@andrevinsky
Last active March 26, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrevinsky/76f14446107e842f26753646750c5196 to your computer and use it in GitHub Desktop.
Save andrevinsky/76f14446107e842f26753646750c5196 to your computer and use it in GitHub Desktop.
const range = function* (
from,
to,
step = from > to ? -1 : 1
) {
if (
step < 0
? from <= to
: from >= to
) {
return;
}
yield from;
return yield* range(from + step, to, step);
};
const range = (start, stop, step = 1) =>
Array(Math.ceil((stop - start) / step))
.fill(start)
.map((x, y) => x + y * step);
//Which i'm using with:
for (y of range(minY, maxY)) {
//foo
}
function* range(from, to, step=1) {
const sign = Math.sign(step)
for (let i = 0; (from + i * step) * sign < to * step; i++) {
yield from + i * step
}
}
Array.from(range(5, 1, -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment