From: Karol Skocik
Subject: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <1121440888.482280.238130@f14g2000cwb.googlegroups.com>
Hi,
  this is fine :

CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
:adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
0)))
T

while this is not :

CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
:adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
10)))
NIL

seems to me, that when no update is done to the input sequence, it's
fine, but when remove-if updates the input vector which has
fill-pointer, the result vector does not contain fill-pointer for some
reason.

is this meant to be so - or is there any reason for it? how to do it
other way?

thank you,
  karol skocik

From: Edi Weitz
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <u8y08jdaq.fsf@agharta.de>
On 15 Jul 2005 08:21:28 -0700, "Karol Skocik" <············@gmail.com> wrote:

>   this is fine :
>
> CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
> :adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
> 0)))
> T
>
> while this is not :
>
> CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
> :adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
> 10)))
> NIL
>
> seems to me, that when no update is done to the input sequence, it's
> fine, but when remove-if updates the input vector which has
> fill-pointer, the result vector does not contain fill-pointer for
> some reason.
>
> is this meant to be so - or is there any reason for it?

The CLHS entry for REMOVE-IF says:

  "If sequence is a vector, the result might or might not be simple,
   and might or might not be identical to sequence."

So, obviously you can't rely on the result to have a fill pointer.

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Pascal Costanza
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <3jq3p6Fqtnf5U1@individual.net>
Karol Skocik wrote:
> Hi,
>   this is fine :
> 
> CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
> :adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
> 0)))
> T
> 
> while this is not :
> 
> CL-USER> (array-has-fill-pointer-p (remove-if #'oddp (make-array 10
> :adjustable t :initial-contents '(0 1 2 3 4 5 6 7 8 9) :fill-pointer
> 10)))
> NIL
> 
> seems to me, that when no update is done to the input sequence, it's
> fine, but when remove-if updates the input vector which has
> fill-pointer, the result vector does not contain fill-pointer for some
> reason.
> 
> is this meant to be so - or is there any reason for it? how to do it
> other way?

remove-if doesn't do anything to its argument. In general, it returns a 
new sequence with the changed contents. There is no guarantee whether 
the new sequence will have the same features as the input sequence, so 
this means that in general, you cannot rely on the fact that the result 
has, for example, a fill-pointer.

What you see happen in the first case is this: remove-if is allowed to 
return its input argument when the result would be the same (equal) 
anyway. Since the fill-pointer in the first case is 0, remove-if doesn't 
see the elements, so by definition cannot do anything interesting. So it 
just returns its input argument, which of course retains the fact that 
it has a fill-pointer.


I hope this helps.


Pascal

-- 
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/
From: Karol Skocik
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <1121442853.238870.322210@g49g2000cwa.googlegroups.com>
ok, i understand now. so i need to duplicate the input sequence and put
is as input to delete-if, which will just alter the contents, not the
properties of the sequence.

however, i was looking to copy-seq, and it unfortunately also drops the
fill-pointer. is there any general way  to make duplicates of
structures or i need to do it manually?

thank you,
  karol skocik
From: Karol Skocik
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <1121444086.069940.167990@g43g2000cwa.googlegroups.com>
i got it - copy-structure.
but why is it not in CLTL2 by Steele?  :-(

bye
From: Edi Weitz
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <uvf3chwir.fsf@agharta.de>
On 15 Jul 2005 09:14:46 -0700, "Karol Skocik" <············@gmail.com> wrote:

> i got it - copy-structure.

COPY-STRUCTURE copies structures, not sequences.

> but why is it not in CLTL2 by Steele?  :-(

CLtL2 is not the final definition of the ANSI standard.  Consult the
CLHS instead.

  <http://www.lispworks.com/documentation/HyperSpec/index.html>

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Pascal Costanza
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <3jq6keFr1he1U1@individual.net>
Karol Skocik wrote:
> i got it - copy-structure.
> but why is it not in CLTL2 by Steele?  :-(

copy-structure doesn't copy arrays, at least not as specified by ANSI 
Common Lisp.

CLtL2 describes the state of Common Lisp as of 1989. Some changes have 
been made between then and the final release of ANSI Common Lisp in 1994/5.


Pascal

-- 
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/
From: Karol Skocik
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <1121445651.859468.12960@g44g2000cwa.googlegroups.com>
ok, thanks. i realized after i post it that arrays are built-in, not
done by defstruct. 

have a nice day,
  karol skocik
From: Pascal Costanza
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <3jq5ljFr346uU1@individual.net>
Karol Skocik wrote:
> ok, i understand now. so i need to duplicate the input sequence and put
> is as input to delete-if, which will just alter the contents, not the
> properties of the sequence.

Nope, this doesn't necessarily work either. delete-if is not required, 
just allowed to change its input argument. If you want a destructive 
operation, you need to do it "manually" by iterating over the array and 
copying elements from old locations to new locations. sebseq and replace 
may be of help, though.

> however, i was looking to copy-seq, and it unfortunately also drops the
> fill-pointer. is there any general way  to make duplicates of
> structures or i need to do it manually?

A way to do what you seem to want is this:

(make-array (array-dimensions old-array)
   ... some properties, like fill-pointer ...
   :initial-contents (loop for element across old-array
                           when (some-predicate-p element)
                           collect element))


Pascal

-- 
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/
From: Pascal Bourguignon
Subject: Re: why is remove-if changing the type of input sequence?
Date: 
Message-ID: <87fyugdjvl.fsf@thalassa.informatimago.com>
Pascal Costanza <··@p-cos.net> writes:

> Karol Skocik wrote:
>> ok, i understand now. so i need to duplicate the input sequence and put
>> is as input to delete-if, which will just alter the contents, not the
>> properties of the sequence.
>
> Nope, this doesn't necessarily work either. delete-if is not required,
> just allowed to change its input argument. If you want a destructive
> operation, you need to do it "manually" by iterating over the array
> and copying elements from old locations to new locations. sebseq and
> replace may be of help, though.
>
>> however, i was looking to copy-seq, and it unfortunately also drops the
>> fill-pointer. is there any general way  to make duplicates of
>> structures or i need to do it manually?
>
> A way to do what you seem to want is this:
>
> (make-array (array-dimensions old-array)
>    ... some properties, like fill-pointer ...
>    :initial-contents (loop for element across old-array
>                            when (some-predicate-p element)
>                            collect element))
>

(defun copy-array (array &key copy-fill-pointer copy-adjustable
                   copy-displacement)
  (when copy-displacement
    (multiple-value-bind (disto disoff) (array-displacement array)
      (when disto
        (return-from copy-array
          (make-array (array-dimensions array)
                      :element-type (array-element-type array)
                      :displaced-to disto
                      :displaced-index-offset disoff
                      :adjustable (when copy-adjustable 
                                    (adjustable-array-p array))
                      :fill-pointer (when copy-fill-pointer
                                      (fill-pointer array)))))))
  (let ((copy (make-array (array-dimensions array)
                          :adjustable (when copy-adjustable 
                                        (adjustable-array-p array))
                          :fill-pointer (when copy-fill-pointer
                                          (fill-pointer array))
                          :element-type (array-element-type array))))
    (dotimes (i (array-total-size copy))
      (setf (row-major-aref copy i) (row-major-aref array i)))
    copy))


(copy-array my-array :copy-fill-pointer t :copy-adjustable t
                     :copy-displacement t)


-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
__Pascal Bourguignon__                     http://www.informatimago.com/