1

Is there any idiomatic way to do some thing similar to what the -> macro does, but with a dynamic list of functions?

I.e. apply every function in a vector to the output of the last function (all functions take one argument)

2 Answers 2

3

A typical idiom is

(reduce #(%2 %) init fns)

For example,

(reduce #(%2 %) 0 [inc #(* 40 %) inc inc]) 
;=> 42
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the build-in comp function. You will want something like:

(defn f [fns & args]
   (let [all-fns (apply comp (reverse fns))]
      (apply all-fns args)))

And use it like:

(f [+ -] 1 2 3)
; -6

1 Comment

As we're told that fns is a vector, prefer rseq to reverse?

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.