Search code examples
vectorclojureappendprepend

What is the idiomatic way to prepend to a vector in Clojure?


Prepending to a list is easy:

user=> (conj '(:bar :baz) :foo)
(:foo :bar :baz)

Appending to a vector is easy:

user=> (conj [:bar :baz] :foo) 
[:bar :baz :foo]

How do I (idiomatically) prepend to a vector, while getting back a vector? This does not work as it returns a seq, not a vector:

user=> (cons :foo [:bar :baz])     
(:foo :bar :baz)

This is ugly (IMVHO):

user=> (apply vector (cons :foo [:bar :baz])) 
[:foo :bar :baz]

Note: I basically just want a datastructure that I can append and prepend to. Appending to large lists should have a large performance penalty, so I thought of vectors..


Solution

  • Vectors are not designed for prepending. You have only O(n) prepend:

    user=> (into [:foo] [:bar :baz])
    [:foo :bar :baz]
    

    What you want is most likely a finger tree.