Question:What’s a good way to create a variadic ‘for’ in Clojure?
I wrote this out:
So I can use this like:
(defmacro variadic-for [f colls] (let [bindings (map (fn [coll] [(gensym) coll]) colls)] `(for ~(vec (apply concat bindings)) (~f ~@(map first bindings)))))
and the result will be every possible sum, where each given collection represents the possible values for that binding.
(variadic-for + [  [3 4]])
Is there a better way to do this? I don’t know of anything in the Clojure core that is used to create all permutations in this way, other than ‘for’.
(defn variadic-for [f colls] (map #(apply f %) (apply clojure.math.combinatorics/cartesian-product colls))) (variadic-for + [  [3 4]]) => (6 7)
If you have better answer, please add a comment about this, thank you!