Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why clojure's vector function definition is so verbose?

Tags:

clojure

I'm really curious to see why vector's implementation is so verbose? What's the reason it can't just do [], [a] and [a & args]?

Here is what I get from clj-1.4.0.

=> (source vector)
(defn vector
  "Creates a new vector containing the args."
  {:added "1.0"
   :static true}
  ([] [])
  ([a] [a])
  ([a b] [a b])
  ([a b c] [a b c])
  ([a b c d] [a b c d])
  ([a b c d & args]
     (. clojure.lang.LazilyPersistentVector (create (cons a (cons b (cons c (cons d args))))))))
nil
like image 668
Alex Dong Avatar asked Jul 19 '12 23:07

Alex Dong


1 Answers

The first few cases have direct calls to make them faster because they are the most commonly called. The rare cases where it is called with many arguments may require more calls and hence more time, but this keeps the common cases concise. It was a deliberate speed, verbosity tradeoff. It also makes the use of the function clear from looking at the argument list without cluttering people's IDEs with a huge list of arities.

Most of Clojure's core functions have similar signatures.

like image 177
Arthur Ulfeldt Avatar answered Nov 03 '22 01:11

Arthur Ulfeldt