Skip to content

Instantly share code, notes, and snippets.

@tho-graf
Created June 14, 2019 09:07
Show Gist options
  • Save tho-graf/b2a9bb688a02d7b15e27a42e1902a6c7 to your computer and use it in GitHub Desktop.
Save tho-graf/b2a9bb688a02d7b15e27a42e1902a6c7 to your computer and use it in GitHub Desktop.
range-generator
function* range(fromInclusive, toExclusive, step = 1) {
if (toExclusive == undefined) {
toExclusive = fromInclusive;
fromInclusive = 0;
}
for (let i = fromInclusive; (toExclusive - i) * step > 0; i += step) {
yield i;
}
}
console.log([...range(0, 10, 4)]);
console.log([...range(20, 10, -2)]);
console.log([...range(-10, 0)]);
console.log([...range(-10)]);
console.log([...range(0, 10)]);
console.log([...range(10)]);
for (const i of range(0, 10)) {
console.log(i);
}
function* range(fromInclusive, toExclusive, step = 1) {
if (toExclusive == undefined) {
toExclusive = fromInclusive;
fromInclusive = 0;
}
for (let i = fromInclusive; (toExclusive - i) * step > 0; i += step) {
yield i;
}
}
console.log([...range(0, 10, 4)]);
console.log([...range(20, 10, -2)]);
console.log([...range(-10, 0)]);
console.log([...range(-10)]);
console.log([...range(0, 10)]);
console.log([...range(10)]);
for (const i of range(0, 10)) {
console.log(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment