Why does this function not work as expected?
(defn my-juxt
[& fns]
(if (= 1 (count fns))
(fn [& a] (list (apply (first fns) a)))
(fn [& a]
(cons (apply (first fns) a) ((my-juxt (rest fns)) a)))))
Note: This works –
(defn new-juxt
[& fns]
(fn [& a]
(map #(apply % a) fns)))
The problem is in how varargs are used.
my-juxthas params[& fns]while it’s given[fns]in the last string. The same is with the function it returns as a result: it expects[& a]while provided[a].The code below will work (please note two extra
apply‘s there)