Is it possible to do var args for an anonymous function in Clojure?
For example, how do I turn:
(#(reduce + (map * %1 %2 %3)) [1 2 3] [4 5 6] [7 8 9])
into something like,
(#(reduce + (map * [& args])) [1 2 3] [4 5 6] [7 8 9])
This solves the problem:
user> ((fn[& args] (reduce + (apply map * args))) [1 2 3] [4 5 6] [7 8 9])
270
or
user> (#(reduce + (apply map * %&)) [1 2 3] [4 5 6] [7 8 9])
270
Use the apply
function
I'm not aware of a way to do this with the #(...)
syntax, but here is your example using fn
((fn [& args] (reduce + (apply map * args))) [1 2 3] [4 5 6] [7 8 9])
You can use %&
to get the rest argument in the #(...)
form, resulting in
(#(reduce + (apply map * %&)) [1 2 3] [4 5 6] [7 8 9])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With