Skip to content

Instantly share code, notes, and snippets.

@SVMBrown
Created May 6, 2015 14:33
Show Gist options
  • Save SVMBrown/d7eeb6c41790315662d4 to your computer and use it in GitHub Desktop.
Save SVMBrown/d7eeb6c41790315662d4 to your computer and use it in GitHub Desktop.
Cartesian Product
(defn cartesianProduct [a b]
(clojure.set/union
(map
#(into []
(list*
(first b)
(if (coll? %)
%
(list %)
)
)
)
a
)
(if (empty? (rest b))
'()
(cartesianProduct a (rest b))
)
)
)
(defn genProduct [& args]
(if (empty? (rest (rest args)))
(cartesianProduct (first (rest args)) (first args))
(cartesianProduct (apply genProduct (rest args)) (first args))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment