Here’s clojure’s definition of 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))))))))
Why are there so many cases? Or, if there are so many, why aren’t there more?
My guess is that it’s striking a balance between implementation efficiency and probability, but I don’t quite see how this would be more efficient.
4 seems to strike a balance of efficiency between when there are lots of arguments and when there are not many arguments.
As an example:
Running a test with 4 elements:
And then with 5:
And with 8 (so all of the functions are using the var-args case):