I think there is a way to do this - I just can't find the exact syntax.
I want to map a function that takes three parameters on a list of tuples of size three. Something like:
(def mylist '((1 2 3)(3 4 5)))
(defn myfunc [a b c] (println "this is the first value in this tuple: " a))
(map myfunc mylist)
Can someone give me the precise syntax?
2 Answers 2
You just need a pair of square braces in there to destructure the nested list elements.
(defn myfunc
[[a b c]]
(println "this is the first value in this tuple: " a))
Be aware, however, that because map returns a lazy-seq, you may not get the side-effects you're after here, unless you force evaluation of the seq with doall, or inspect the seq in the REPL.
Documentation: http://clojure.org/special_forms#Special Forms--Binding Forms (Destructuring)
Comments
d11wtq's answer is a very good one, and it's the right way to go, unless you already have a function you want to map.
So, if myfunc is external function, it's better to use apply rather than write additional wrapper:
(map (partial apply myfunc) mylist)