It is correct that vectors aren't a suitable choice for datastructures that 
need "random-access-removal". The problem is that you seem to need both 
fast index lookup and be able to access elements "after" removed elements 
quickly even when there are holes in the backing array.

There are some solutions. One is to use clojure.core/subvec, to keep the 
parts of the vector that should be kept, and concat these two sub vecs. I'm 
not sure if the removed element will be garbage collected - subvec reuses 
the structure of original vector, no copying seems to be done, so no 
"removed" element will be garbage collected until all subvecs of the 
original vector are realized as some other collection! If you are unsure 
about why (or, equally likely, prove me wrong), please refer to 
http://hypirion.com/musings/understanding-persistent-vector-pt-1 

There are also the finger-trees, which are not as wide as PersistentVector 
and are more suitable for splitting etc. Probably one could construct a 
datastructure more suitable for random access removal with that.

https://github.com/clojure/data.finger-tree

If the hotspot of your application is to quickly lookup things in vectors 
and quickly remove individual elements it feels like it exists some clever 
datastructure that, given an virtual index in the (partially shadowed) 
vector, increments the lookup index for each removed element before the 
virtual position, to compensate for the holes in the vector, but that was 
probably not really your question.

However, the problem is to find datastructures which both allow quick 
lookup and can keep track of the lookup when parts of the datastructure are 
marked as removed.

What ever you do, make sure to profile it so that you don't hunt for 
performance in vain.

/Linus

On Wednesday, August 26, 2015 at 12:12:28 AM UTC+2, Georgi Danov wrote:
>
> How about filtering? 
> BTW I don't see how it would help converting to array - what would be the 
> solution then?
>
> On Tuesday, August 25, 2015 at 7:06:30 PM UTC+2, Hussein B. wrote:
>>
>> Hi,
>>
>> For a vector like [A B C D E], how to remove an element to a specific 
>> location? For example [A D B C E] ?
>>
>> I thought about converting the vector into array but I would feel bad if 
>> I did that.
>>
>> What would be the idiomatic way to do that in Clojure?
>>
>> Thanks for help and time.
>>
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to