(comp)
(comp f)
(comp f g)
(comp f g h)
(comp f1 f2 f3 & fs)
Takes a set of functions and returns a fn that is the composition
of those fns. The returned fn takes a variable number of args,
applies the rightmost of fns to the args, the next
fn (right-to-left) to the result, etc.
Example
(do
["Combine (compose) filter and count to get the number of vowels"]
(def count-if (comp count filter))
(count-if #{\a \e \i \o \u} "clojure"))
Source
(defn
comp
"Takes a set of functions and returns a fn that is the composition\n of those fns. The returned fn takes a variable number of args,\n applies the rightmost of fns to the args, the next\n fn (right-to-left) to the result, etc."
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
([x y z & args] (f (apply g x y z args)))))
([f g h]
(fn
([] (f (g (h))))
([x] (f (g (h x))))
([x y] (f (g (h x y))))
([x y z] (f (g (h x y z))))
([x y z & args] (f (g (apply h x y z args))))))
([f1 f2 f3 & fs]
(let
[fs (reverse (list* f1 f2 f3 fs))]
(fn
[& args]
(loop
[ret (apply (first fs) args) fs (next fs)]
(if fs (recur ((first fs) ret) (next fs)) ret))))))