Reputation: 3312
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)))
Upvotes: 1
Views: 165
Reputation: 34312
The problem is in how varargs are used. my-juxt
has 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)
(defn my-juxt
[& fns]
(if (= 1 (count fns))
(fn [& a] (list (apply (first fns) a)))
(fn [& a]
(cons (apply (first fns) a)
(apply (apply my-juxt (rest fns)) a)))))
Upvotes: 6