( ) comp:
(defn comp [& fs]
(reduce (fn [result f]
(fn [& args]
(result (apply f args))))
identity
fs))
, , .
(defn chain [f g]
(fn [& args]
(f (apply g args))))
chain comp, , .
((chain inc inc) 1) ;=> 3
((chain rest reverse) [1 2 3 4]) ;=> (3 2 1)
((chain inc inc inc) 1) ;=> ArityException
comp atop chain , reduce .
(defn comp [& fs]
(reduce chain identity fs))
, . ..
, :
((comp #(.toUpperCase %) #(apply str %) take) 5 "hello world") ;=> "HELLO"
, chain (no reduce):
((chain identity
(chain (chain
take))
5 "hello world")
;=> "HELLO"
reduce . ( , Clojure):
def reduce(f, init, seq):
result = init
for item in seq:
result = f(result, item)
return result
. , reduce , , , , (, , , ).