function | since v0.0-927 | clojure.core/comp | Edit |
(comp)
(comp f)
(comp f g)
(comp f g h)
(comp f1 f2 f3 & fs)
Takes a set of functions and returns a function that is the composition of those functions.
The returned function takes a variable number of arguments, applies the
rightmost of fns
to the arguments, whose result is subsequently applied to
the next left function, and so on.
((comp a b c) x y)
= (a (b (c x y)))
(def f (comp str inc +))
(f 1 2 3)
;;=> "7"
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.
(defn comp
([] 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))))))