Created
June 26, 2018 01:33
-
-
Save Ariex/a67abef7891d8e95bf5540e63af1743a to your computer and use it in GitHub Desktop.
generate full combination of an array in js generator
This file contains hidden or 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 *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