3

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

4

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)

Sign up to request clarification or add additional context in comments.

Comments

3

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.