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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With