Skip to content

Instantly share code, notes, and snippets.

@gobwas
Last active January 4, 2016 02:29
Show Gist options
  • Save gobwas/8555242 to your computer and use it in GitHub Desktop.
Save gobwas/8555242 to your computer and use it in GitHub Desktop.
Returns combinations of given values.
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;
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