Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a list of functions to a corresponding list of data in Clojure

Tags:

clojure

So I have a list of functions and a list of data:

[fn1 fn2 fn3] [item1 item2 item3]

What can I do to apply each function to its corresponding data item:

[(fn1 item1) (fn2 item2) (fn3 item3)]

Example:

[str #(* 2 %) (partial inc)]   [3 5 8]

=> ["3" 10 9]
like image 209
leontalbot Avatar asked May 15 '13 17:05

leontalbot


1 Answers

You can use map

(map #(%1 %2) [str #(* 2 %) (partial inc)] [3 5 8])
("3" 10 9)

If you need a vector back, you can (apply vector ...)

(apply vector (map #(%1 %2) [str #(* 2 %) (partial inc)] [3 5 8]))
["3" 10 9]

Disclaimer: I don't know much Clojure, so there would probably be better ways to do this.

like image 108
Dogbert Avatar answered Sep 29 '22 13:09

Dogbert