Last active
January 4, 2016 02:29
-
-
Save gobwas/8555242 to your computer and use it in GitHub Desktop.
Returns combinations of given values.
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
combine = (list, index = 0, combination = [], result = []) -> | |
if (list[index]) | |
for value in list[index] | |
combination[index] = value; | |
combine(list, index + 1, combination, result); | |
else | |
result.push(combination[..]); | |
return result; |
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 combine(list, index, combination, result) { | |
if (index == void 0) { | |
index = 0; | |
combination = []; | |
result = []; | |
} | |
if (list[index]) { | |
for (var i = 0; i < list[index].length; i++) { | |
combination[index] = list[index][i]; | |
combine(list, index + 1, combination, result); | |
} | |
} else { | |
result.push(combination.slice(0)); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment