Arity overload in Clojure source

Here's the source code for update-in

:

(defn update-in
 ([m [k & ks] f]
   (if ks
    (assoc m k (update-in (get m k) ks f))
    (assoc m k (f (get m k)))))
 ([m [k & ks] f a]
   (if ks
    (assoc m k (update-in (get m k) ks f a))
    (assoc m k (f (get m k) a))))
 ([m [k & ks] f a b]
   (if ks
    (assoc m k (update-in (get m k) ks f a b))
    (assoc m k (f (get m k) a b))))
 ([m [k & ks] f a b c]
   (if ks
    (assoc m k (update-in (get m k) ks f a b c))
    (assoc m k (f (get m k) a b c))))
 ([m [k & ks] f a b c & args]
   (if ks
    (assoc m k (apply update-in (get m k) ks f a b c args))
    (assoc m k (apply f (get m k) a b c args)))))

      

As far as I know (and now I don't really much), this always gives the same result:

(defn my-update-in2
 ([m [k & ks ] f & args]
  (if ks
   (assoc m k (apply update-in (get m k) ks f args))
   (assoc m k (apply f (get m k) args)))))

      

My question is, why isn't this update-in

(and many other Clojure features) implemented this way? I would guess that there are performance issues i.e. not using apply

faster.

+3


source to share


1 answer


Yes, you guessed it right: some of the artifacts exist because of the cost of performance apply

.



Having explicit appearances for the most common cases (for example, up to 3 function arguments f

) improves performance as it translates to a direct function call.

+6


source







All Articles