Created
December 30, 2019 16:29
-
-
Save kylehovey/3c480011ae942ef6d3c270248cab439d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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