Skip to content

Instantly share code, notes, and snippets.

@fronterior
Last active April 22, 2022 11:41
Show Gist options
  • Save fronterior/da605ac5190b9933b407b8bdf7376268 to your computer and use it in GitHub Desktop.
Save fronterior/da605ac5190b9933b407b8bdf7376268 to your computer and use it in GitHub Desktop.
function* numberOfCases(...args) {
const stack = [[[], args]];
while (stack.length) {
const target = stack.shift();
if (target) {
const [result, rest] = target;
for (let i = 0; i < rest.length; i++) {
let r;
yield r = [...result, rest[i]];
stack.push([r, rest.filter((_, index) => index !== i)]);
}
}
}
}
iter = numberOfCases(...'12345'.split(''));
[...iter];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment