From: Slobodan Blazeski on
I have an adjustable array :

> (setq foo (make-array 0 :element-type 'fixnum
:fill-pointer 0 :adjustable t))
; added some elements
> foo
#(1 2 1 3 4 1 1 5 101)


How to remove say third element?

Slobodan
From: Ron Garret on
In article
<b63161f5-8ec6-42f0-b489-469e6f3f0293(a)x22g2000yqx.googlegroups.com>,
Slobodan Blazeski <slobodan.blazeski(a)gmail.com> wrote:

> I have an adjustable array :
>
> > (setq foo (make-array 0 :element-type 'fixnum
> :fill-pointer 0 :adjustable t))
> ; added some elements
> > foo
> #(1 2 1 3 4 1 1 5 101)
>
>
> How to remove say third element?
>
> Slobodan

This functionality is not built-in to CL. You have to roll your own.
Here's a general way to do it if you don't care much about efficiency:

(defun delete-nth (sequence n)
(let ((g (gensym)))
(setf (elt sequence n) g)
(delete g sequence)))

A better way would be to manually slide all the downstream elements one
position left and then adjust the size of the vector.

rg
From: Slobodan Blazeski on
On Mar 6, 9:43 pm, Ron Garret <rNOSPA...(a)flownet.com> wrote:
> In article
> <b63161f5-8ec6-42f0-b489-469e6f3f0...(a)x22g2000yqx.googlegroups.com>,
>  Slobodan Blazeski <slobodan.blaze...(a)gmail.com> wrote:
>
> > I have an adjustable array :
>
> > > (setq foo  (make-array 0  :element-type 'fixnum
> >        :fill-pointer 0 :adjustable t))
> > ; added some elements
> > > foo
> > #(1 2 1 3 4 1 1 5 101)
>
> > How to remove say third element?
>
> > Slobodan
>
> This functionality is not built-in to CL.  You have to roll your own.  
> Here's a general way to do it if you don't care much about efficiency:
>
> (defun delete-nth (sequence n)
>   (let ((g (gensym)))
>     (setf (elt sequence n) g)
>     (delete g sequence)))
>
> A better way would be to manually slide all the downstream elements one
> position left and then adjust the size of the vector.
>
> rg
Ok will use below for now.

(defun delete-at (i seq)
(let ((slide (subseq seq (1+ i)))
(num (1- (fill-pointer seq))))
(replace seq slide :start1 i)
(adjust-array seq num
:fill-pointer num)))


STYLE-WARNING: redefining DELETE-AT in DEFUN
; Evaluation aborted.
DELETE-AT
CL>
CL> (delete-at 2 foo)
#(0 1 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 9)
CL> (defun delete-at (i seq)
(let ((slide (subseq seq (1+ i)))
(num (1- (fill-pointer seq))))
(replace seq slide :start1 i)
(adjust-array seq num
:fill-pointer num)))

DELETE-AT
CL> (delete-at 2 foo)
#(0 1 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 9)
CL> (setq foo (make-array 0 :fill-pointer t :adjustable t))
#()
CL> (dotimes (i 10) (vector-push-extend i foo))
NIL
CL> (delete-at 2 foo)
#(0 1 3 4 5 6 7 8 9)
CL> (delete-at 5 foo)
#(0 1 3 4 5 7 8 9)


Slobodan
From: Zach Beane on
Slobodan Blazeski <slobodan.blazeski(a)gmail.com> writes:

> Ok will use below for now.
>
> (defun delete-at (i seq)
> (let ((slide (subseq seq (1+ i)))
> (num (1- (fill-pointer seq))))
> (replace seq slide :start1 i)
> (adjust-array seq num
> :fill-pointer num)))

REPLACE has sane behavior when both the source and target are the same
vector, and the ranges overlap, e.g.

(replace vector vector :start1 i :start2 (1+ n))

Zach
From: Slobodan Blazeski on
On Mar 6, 10:33 pm, Zach Beane <x...(a)xach.com> wrote:
> Slobodan Blazeski <slobodan.blaze...(a)gmail.com> writes:
> > Ok will use below for now.
>
> >  (defun delete-at (i seq)
> >       (let ((slide (subseq seq (1+ i)))
> >        (num (1- (fill-pointer seq))))
> >         (replace seq slide  :start1 i)
> >    (adjust-array seq num
> >                  :fill-pointer num)))
>
> REPLACE has sane behavior when both the source and target are the same
> vector, and the ranges overlap, e.g.
>
>   (replace vector vector :start1 i :start2 (1+ n))
>
> Zach

If sequence-1 and sequence-2 are the same object and the region being
modified overlaps the region being copied from, then it is as if the
entire source region were copied to another place and only then copied
back into the target region. http://www.lispworks.com/documentation/lw50/CLHS/Body/f_replac.htm

Nice thanks

Slobodan

 |  Next  |  Last
Pages: 1 2
Prev: How to test the non exported symbols
Next: ECL 10.3.1