2016-04-02 13 views
3

Ich habe eine Funktion:Karte eine Funktion, die auf einen Vektor von drei Elementvektoren in Clojure drei Parameter nimmt

(defn myfunc [x y z] ....does something interesting here) 

I einen Vektor von Datengruppen haben:

(def mydata [[:a1 :b1 :c1] [:a2 :b2 :c2] [:a3 :b3 :c3]]) 

Ich möchte rufen myfunc mit dreimal jeweils wie:

(myfunc :a1 :b1 :c1) 
(myfunc :a2 :b2 :c2) 
(myfunc :a3 :b3 :c3) 

habe ich eine vage Verständnis diese Zuordnung umfassen wird und deconstructi ng? Anders als das ich stecken bin ..

Antwort

6

erster wichtiger Punkt zu verstehen ist, dass

(my-func :a1 :b1 :c1) 

ideologisch gleich zu

(apply my-func [:a1 :b1 :c1]) 

Nachdem Sie mit ihm in Ordnung sind, dann ist es einfach, was zu implementieren Sie müssen mit Kombination von map, partial und apply:

(def mydata [[:a1 :b1 :c1] [:a2 :b2 :c2] [:a3 :b3 :c3]]) 

(map (partial apply my-func) mydata) 
Verwandte Themen