Clojure :: arity-overloaded functions calling each other -
examples of clojure arity-overloading on functions next (taken cookbook):
(defn argcount ([] 0) ; 0 arguments ([x] 1) ; 1 argument ([ x & args] (inc (count args)))) ; list of arguments
... utilize form doesn't seem allow functions of lower arity phone call functions of higher arity default values (that's mutual idiom in java). other special form used ?
there's way express higher arity arguments in way doesn't need refer other arities using higher order functions , map
/ reduce
. in case it's pretty simple:
(defn argcount ([] 0) ([x] 1) ([x & args] (reduce + 1 (map (constantly 1) args))))
notice general form of look is:
(reduce reducing-function arity-1-value (map mapping-function rest-of-args))
you can't way, works surprisingly big proportion of multi-argument functions. gains advnatages of laziness using map
, can crazy things pass 10 1000000 arguments function little fear:
(apply argcount (take 10000000 (range))) => 10000000
try in other languages , stack toast :-)
clojure
No comments:
Post a Comment