Skip to content

Instantly share code, notes, and snippets.

@maxweber
Last active August 31, 2015 10:49
Show Gist options
  • Save maxweber/633d357641b8af50c3fe to your computer and use it in GitHub Desktop.
Save maxweber/633d357641b8af50c3fe to your computer and use it in GitHub Desktop.
Cartesian Product
(def data [[1 2] [11 22] [111 222]])
(defn combine [a b]
(mapcat
(fn [x]
(map
(fn [y]
(flatten [x y]))
b))
a))
(defn cartesian-product [sets]
(reduce
(fn [r set]
(combine r set))
(first sets)
(rest sets)))
(cartesian-product data)
var data = [[1,2], [11,22], [111,222]];
var combine = function (a, b) {
return mori.mapcat(
function(x) {
return mori.map(
function (y) {
return mori.flatten([x,y]);}, b);
}, a);
};
var cartesian_product = function (sets) {
return mori.reduce(
function(r, set) {
return combine(r,set);
}, sets[0], mori.rest(sets));
}
console.log(mori.toJs(cartesian_product(
data)));
@maxweber
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment