Reputation: 143
I'm a newbie to Clojure and am a bit confused about how functions are called.
Let's say I have this function
(defn foo [& fns]
#(apply (first fns) %&))
When I call ((foo + max min) 2 3 5 1 6 4)
it will return 21
.
When I define the function like this,
(defn foo [& fns]
(conj [] #(apply (first fns) %&)))
I was expecting ((foo + max min) 2 3 5 1 6 4)
to return [21]
but instead I get
Execution error (ArityException) at user/eval2258 (REPL:1).
Wrong number of args (6) passed to: clojure.lang.PersistentVector
Why is that? And how would I fix it?
Upvotes: 0
Views: 67
Reputation: 37073
(defn foo [& fns] (conj [] #(apply (first fns) %&)))
I was expecting
((foo + max min) 2 3 5 1 6 4)
to return[21]
...
Your original foo
returns a function. Now
you return a vector with a function in it. Now when calling it, you are
calling the vector, thus the error message.
user=> ([] 1 2 3)
Execution error (ArityException) at user/eval150 (REPL:1).
Wrong number of args (3) passed to: clojure.lang.PersistentVector
To correct this, you would have put the result of the apply into the
vector, but still return a function. But since you are not yet using
all passed in fns
it's not clear, what to suggest from here.
This would give you the result, you are looking for:
(defn foo [& fns]
#(vector (apply (first fns) %&)))
Upvotes: 4