(defn insert [v at el]
  (flatten (assoc v at [el (v at)]))

You get a lazy seq back, so further operations are probably not as
performant as they should be.  But that would be as performant as a
regular assoc for just that one operation.  What you do from then
on.....

Mark

On Aug 26, 10:33 am, Chouser <chou...@gmail.com> wrote:
> On Thu, Aug 26, 2010 at 12:43 PM, David Nolen <dnolen.li...@gmail.com> wrote:
> > On Thu, Aug 26, 2010 at 11:53 AM, Chouser <chou...@gmail.com> wrote:
>
> >> On Thu, Aug 26, 2010 at 9:44 AM, Jon Seltzer <seltzer1...@gmail.com>
> >> wrote:
> >> > I know what assoc does:
>
> >> > user=> (assoc [\a \b \c] 0 \d)  ;please extend to much larger vector
> >> > with index somewhere in the middle
> >> > [\d \b \c]
>
> >> > but what if I want:
>
> >> > user=> (assoc-x [\a \b \c] 0 \d)  ;is there an assoc-x
> >> > [\a \d \b \c]
>
> >> > I don't see a function that does this.  I'm sure I'm missing it.  I,
> >> > of course, know this could be done with a combination of other
> >> > functions like subvec and/or into but it seems fundamental enough to
> >> > have its function.
>
> >> The internal structure of vectors is such that this cannot be
> >> done efficiently, which is why no function to do it is included
> >> in clojure.core.  You can do it inefficiently like this:
>
> >>    (let [v '[a b c d e f]
> >>          i 3]
> >>      (into (subvec v 0 i) (cons 'X (subvec v i))))
> >>    ;=> [a b c X d e f]
>
> >> That's about as good as you're going to get with vectors, O(n)
> >> where n is (- (count v) i).
>
> >> --Chouser
> >>http://joyofclojure.com/
>
> > How is the performance of your FingerTrees looking? ;)
> > David
>
> I'm actually working on it again, now that the book is requiring less
> time.  When it's ready, I won't be shy about announcing it. :-)
>
> --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 at
http://groups.google.com/group/clojure?hl=en

Reply via email to