ps-aux
ps-aux

Reputation: 12174

into list vs. into vector in Clojure

Can you explain this behaviour in Clojure ?

user=> (into [1 2 3] ["a" "b"])
[1 2 3 "a" "b"]

but

user=> (into '(1 2 3) ["a" "b"])
("b" "a" 1 2 3)

It is understandable that into with vector appends the items but why using into with list first reverts the items order and then prepends it to the list ?

Upvotes: 3

Views: 197

Answers (1)

Lee
Lee

Reputation: 144206

into uses conj to add items into the source collection. conj appends items to the front for lists and to the end for vectors. Clojure lists are immutable singly-linked lists, so adding to the end of the list would be an O(n) operation. Insertion at the front is a constant-time operation.

Upvotes: 8

Related Questions