Skip to content

Instantly share code, notes, and snippets.

@ChrisJefferson
Forked from ijy/cartesian-product.js
Created May 29, 2015 14:34
Show Gist options
  • Save ChrisJefferson/cb8db2a4c67a9506c56c to your computer and use it in GitHub Desktop.
Save ChrisJefferson/cb8db2a4c67a9506c56c to your computer and use it in GitHub Desktop.
function cartesianProductOf() {
return _.reduce(arguments, function(a, b) {
return _.flatten(_.map(a, function(x) {
return _.map(b, function(y) {
return x.concat([y]);
});
}));
}, [ [] ]);
};
cartesianProductOf([1, 2], [3, 4], ['a', 'b']); // [[1,3,"a"],[1,3,"b"],[1,4,"a"],[1,4,"b"],[2,3,"a"],[2,3,"b"],[2,4,"a"],[2,4,"b"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment