Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map function parameters onto nested list in Clojure

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?

like image 651
hawkeye Avatar asked Dec 12 '25 09:12

hawkeye


1 Answers

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)

like image 178
d11wtq Avatar answered Dec 14 '25 11:12

d11wtq