vector-pop and vector-remove

33 views
Skip to first unread message

mazert

unread,
Aug 2, 2015, 7:00:06 AM8/2/15
to racke...@googlegroups.com
Hello,

I have two functions I often use to remove an element from a vector, and
it will be nice to add it directly in the vector module, or in the worst
case into a specific package. Cause I'm sure it will be useful and we
already have such functions for list, but not for vector...

; Remove the ith element of vector v
(define (vector-pop i v)
(vector-append (vector-copy v 0 i) (vector-copy v (+ i 1))))

; Like remove (from list) but for a vector
(define (vector-remove ele v)
(vector-filter-not (λ (x) (equal? x ele)) v))


Of course we have to use the check-vector/index to raise an error if "i"
is out of range (see vector.rkt).


Jens Axel Søgaard

unread,
Aug 2, 2015, 7:04:19 AM8/2/15
to mazert, racke...@googlegroups.com
Hi,

For programs that need to use "vectors whose length can change"
grow vectors are often the right choice:


/Jens Axel





--
You received this message because you are subscribed to the Google Groups "Racket Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-dev+...@googlegroups.com.
To post to this group, send email to racke...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-dev/mpkst6%24koq%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.



--
--
Jens Axel Søgaard

mazert

unread,
Aug 2, 2015, 10:57:00 AM8/2/15
to racke...@googlegroups.com
Le 02/08/2015 13:04, Jens Axel Søgaard a écrit :
> Hi,
>
> For programs that need to use "vectors whose length can change"
> grow vectors are often the right choice:
>
> http://docs.racket-lang.org/data/gvector.html

Ok, I didn't know about this data structure, and effectively you are
right : the performance are better. I tested with a simple program
computing time to remove the first element from a gvector and a vector,
both contains 1000000 elements :

gvector : 7 ms
vector : 102 ms

So that was a very bad idea, thank you for letting me know :)


Jens Axel Søgaard

unread,
Aug 2, 2015, 11:55:35 AM8/2/15
to mazert, racke...@googlegroups.com
Hi,


Here is how I choose which data structure to use in Racket:

DATA STRUCTURE   ACCESS       NUMBER     INDICES
List:            sequential   Variable   not used
Struct:          random       Fixed      names
Vector:          random       Fixed      integer
Growable vector: random       Variable   integer
Hash:            random       Variable   hashable
Splay:           random       Variable   non-integer, total order

--
You received this message because you are subscribed to the Google Groups "Racket Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-dev+...@googlegroups.com.
To post to this group, send email to racke...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages