Skip to content

Instantly share code, notes, and snippets.

@kylehovey
Created December 30, 2019 16:29
Show Gist options
  • Save kylehovey/3c480011ae942ef6d3c270248cab439d to your computer and use it in GitHub Desktop.
Save kylehovey/3c480011ae942ef6d3c270248cab439d to your computer and use it in GitHub Desktop.
function *_range(from, to) {
if (to === undefined) {
yield *_range(0, from);
return;
}
yield [ from ];
if (from >= to) return;
yield *_range(from + 1, to);
}
function chain(...args) {
const next = (
function*() {
for (const acc of this) {
for (const [i] of _range(...args)) {
yield [ ...acc, i ];
}
}
}
).bind(this)();
Object.defineProperty(next, 'range', { value: chain });
return next;
}
const range = (...args) => {
const it = _range(...args);
Object.defineProperty(it, 'range', { value: chain });
return it;
}
for (const [ a, b, c, d, e ] of range(6).range(6).range(6).range(6).range(6)) {
console.log({ a, b, c, d, e });
}
module.exports = { range };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment