Fork me on GitHub
#beginners
<
2023-07-11
>
DeepReef1115:07:38

Why is arity for many functions not just called with &args? For instance:

(source complement)
=>
(defn complement
  "Takes a fn f and returns a fn that takes the same arguments as f,
  has the same effects, if any, and returns the opposite truth value."
  {:added "1.0"
   :static true}
  [f]
  (fn
    ([] (not (f)))
    ([x] (not (f x)))
    ([x y] (not (f x y)))
    ([x y & zs] (not (apply f x y zs)))))
Why not just:
(source complement)
=>
(defn complement
  "Takes a fn f and returns a fn that takes the same arguments as f,
  has the same effects, if any, and returns the opposite truth value."
  {:added "1.0"
   :static true}
  [f]
  (fn
    ([] (not (f)))
    ([x] (not (f x)))
    ([x & zs] (not (apply f x zs)))))
Shouldn't that work as well?

Bob B15:07:03

I believe the short answer is that common (small) arities are unrolled for performance

1
Lum J17:07:46

@U05AV2HF7SQ Thank you for the question!

1
jaihindhreddy05:07:32

We actually only "need" one arity:

(defn complement [f]
  #(not (apply f %&)))