Created
July 19, 2013 12:16
-
-
Save svdmitrij/e7a061b8f29ec37c60e9 to your computer and use it in GitHub Desktop.
Cartesian product
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 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