Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 486
ps-aux Avatar asked Jun 02 '26 17:06

ps-aux


1 Answers

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.

like image 145
Lee Avatar answered Jun 06 '26 03:06

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!