Documentation for conj
(from clojure.org):
conj[oin]. Returns a new collection with the xs
'added'. (conj nil item) returns (item). The 'addition' may
happen at different 'places' depending on the concrete type.
It's more efficient to "add" elements to the end of a vector, while it's more efficient to do so at the beginning of lists. conj
uses whatever is the most efficient for the data structure you give it.
In the examples you give, '(1 2 3)
and (seq [1 2 3])
both implement ISeq
(see documentation for seq?
), while [1 2 3]
doesn't.
Clojure's conj
ultimately calls the cons
method (not to be confused with the cons
function - this method is internal clojure code) on the underlying data structure; for vectors (PersistentVector
), cons
adds elements to the end, while for lists they're added to the front (the cons
method for PersistentList
s returns a new list with the new element as its head, and the existing list as its tail).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…