Skip to content

Instantly share code, notes, and snippets.

@Ariex
Created June 26, 2018 01:33
Show Gist options
  • Save Ariex/a67abef7891d8e95bf5540e63af1743a to your computer and use it in GitHub Desktop.
Save Ariex/a67abef7891d8e95bf5540e63af1743a to your computer and use it in GitHub Desktop.
generate full combination of an array in js generator
(()=>{
function *fullCombination(array) {
function *gen(array, prefix) {
if (array.length === 0) {
yield prefix;
} else {
yield*gen(array.slice(1), [...prefix, array[0]]);
yield*gen(array.slice(1), [...prefix]);
}
}
yield*gen(array, []);
}
for(var item of fullCombination([1,2,3,4,5])){
console.log(item);
}
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment