Can someone explain the behavior of "conj"?

(conj (drop-last "abcde") (last "abcde"))

returns (\e \a \b \c \d)

I'm confusing. In the document conjI notice

"Addition" may occur in different "places" depending on the particular type.

Does this mean that for the LazySeqplace to add a new element is the head? How can I get (\a \b \c \d \e)as a result?

+5
source share
2 answers

'' Addition "may occur in different places, depending on the concrete .

This refers to the behavior of Clojure's permanent collections, which include adding in the most efficient way in terms of performance and underlying implementation.

Vectors are always added to the end of the collection:

user=> (conj [1 2 3] 4)
[1 2 3 4]

, conj , :

user=> (conj '(1 2 3) 4)
(4 1 2 3)

, , LazySeq .

(\a \b \c \d \e) ?

, LazySeq:

(conj (vec (drop-last "abcde"))
      (last "abcde"))
+6

, conj cons IPersistentCollection Clojure Java-. , , , -.

, , .

. , , .

+1

All Articles