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).

I messed up the analysis of the complexity there.  It may
actually be worse than O(n), and there may be a more efficient
solution.

--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 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