I'm trying to chain a few functions in Clojure:
(f4 (f3 (f2 (f1 foo))))
Is there any convenient syntax sugar for this? Something like:
(with-all-of-them foo f1 f2 f3 f4)
I'm trying to chain a few functions in Clojure:
(f4 (f3 (f2 (f1 foo))))
Is there any convenient syntax sugar for this? Something like:
(with-all-of-them foo f1 f2 f3 f4)
Use ->
macro.
(-> foo f1 f2 f3 f4)
Or reduce
:
(reduce #(%2 %1) foo [f1 f2 f3 f4])
There is a threading macro ->
:
(-> foo f1 f2 f3 f4)
Actually your description of with-all-of-them
is very close to comp
, except that comp
returns a function that you must call yourself:
(f4 (f3 (f2 (f1 foo))))
== ((comp f4 f3 f2 f1) foo)
So, with-all-of-them
could be implemented as follows:
(defn with-all-of-them [arg & fs]
((apply comp fs) arg))