On Tue, Apr 27, 2010 at 1:31 PM, Sean Devlin <[email protected]> wrote:
> Is there a built in to insert a value into an "indexed" seq?
>
> For example:
>
> user=> (insert [:a :b :c :d] 2 :q)
> (:a :b :q :c :d)
>
> Not sure if I'm missing something simple...
That's a vector, which cannot efficiently splice internally, so
it's not supported directly. However, you can build a new vector
with your value included:
(apply conj [:a :b] :q [:c :d])
;=> [:a :b :q :c :d]
> Also, why does this work:
> user=> (assoc [:a :b :c :d] 2 :q)
> [:a :b :q :d]
>
> And this doesn't:
> user=> (dissoc [:a :b :c :d] 2)
> #<CompilerException java.lang.ClassCastException:
> clojure.lang.PersistentVector cannot be cast to
> clojure.lang.IPersistentMap (NO_SOURCE_FILE:286)>
>
> Annoying.
Again, vectors cannot efficiently insert or remove items except
from the right-hand end, though as you note items can replaced
internally.
(let [v [:a :b :c :d]]
(into (subvec v 0 2) (subvec v 3)))
;=> [:a :b :d]
There are immutable collections that support both numeric-indexed
lookups and internal splicing, they're just not currently included
with Clojure. See for example finger trees:
http://functionaljava.googlecode.com/svn/artifacts/2.21/javadoc/fj/data/Seq.html
--Chouser
http://joyofclojure.com/
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en