Skip to content

Instantly share code, notes, and snippets.

@svdmitrij
Created July 19, 2013 12:16
Show Gist options
  • Save svdmitrij/e7a061b8f29ec37c60e9 to your computer and use it in GitHub Desktop.
Save svdmitrij/e7a061b8f29ec37c60e9 to your computer and use it in GitHub Desktop.
Cartesian product
function cartProd(paramArray) {
function addTo(curr, args) {
var i, copy,
rest = args.slice(1),
last = !rest.length,
result = [];
for (i = 0; i < args[0].length; i++) {
copy = curr.slice();
copy.push(args[0][i]);
if (last) {
result.push(copy);
} else {
result = result.concat(addTo(copy, rest));
}
}
return result;
}
return addTo([], Array.prototype.slice.call(arguments));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment