Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threading through a vector of functions [duplicate]

Tags:

clojure

I have a vector of functions (def my-func [a b c d]). Each function takes the output of the last function as the input. I want to thread an input through them, how do I do that?

How do I get to the following form (-> in a b c d)?

Thanks, Murtaza

like image 421
murtaza52 Avatar asked Sep 19 '12 10:09

murtaza52


2 Answers

You can use comp but be aware it executes the functions right to left

((comp d c b a) 10)

or

((apply comp my-fns) 10)

will pass 10 to the first function, the result to the next function and so on.

like image 70
M Smith Avatar answered Oct 02 '22 16:10

M Smith


I think you can use the reduce function:

(def fns [inc inc inc])
(reduce (fn [v f] (f v)) 10 fns)
like image 32
DanLebrero Avatar answered Oct 02 '22 14:10

DanLebrero