On Apr 27, 2010, at 2:45 PM, Sean Devlin wrote:

You're right, inserting into a vector is fundamentally slow.
Inserting into a list (must traverse elements) or String (Char Array)
isn't any better.  I get why Clojure doesn't include certain
operations on certain data structures (e.g. assoc on a list), because
it's the wrong tool for the job. However, there are still problems
that require me to use an expensive operation.

Maybe I'm too focused on my current project, and wrong about how much
a typical person would use insert.  Still, its absence seems like an
oversight.


What are missing are finger trees, as Chouser said. Not having insert on vector is not an oversight.

Rich

Sean

On Apr 27, 2:05 pm, Chouser <chou...@gmail.com> wrote:
On Tue, Apr 27, 2010 at 1:31 PM, Sean Devlin <francoisdev...@gmail.com> 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/da ...

--Chouserhttp://joyofclojure.com/

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group 
athttp://groups.google.com/group/clojure?hl=en

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to