[Newbies] Re: How to empty a collection?

2008-02-19 Thread Klaus D. Witzel

On Tue, 19 Feb 2008 16:06:50 +0100, cdrick wrote:

...

I forgot that in my first attemp (and I knew that !)...

col := #(1 2 3) asOrderedCollection.
col  removeAll: col.
^col   returns an OrderedCollection(2)

col  removeAll: col copy is ok.

So, following Bert suggestion, would it be possible to change removeAll:  
from...


removeAll: aCollection
aCollection do: [:each | self remove: each].
^ aCollection
to

removeAll: aCollection
aCollection copy do: [:each | self remove: each].
^ aCollection
or

removeAll: aCollection
aCollection == self
ifTrue: [aCollection copy do: [:each | self remove: each]]
ifFalse: [aCollection do: [:each | self remove: each]].
^ aCollection

or again
removeAll: aCollection
aCollection == self
ifTrue: [self removeAll: aCollection copy]
ifFalse: [aCollection do: [:each | self remove: each]].

Or maybe, if aCollection == self, a warning could be raised ?

What do you think ?


Thumbs down, I do not want anybody copy a collection behind my back  
(however small or large it is) just because remove* has nothing to do with  
copy.



Cédrick



___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-19 Thread nicolas cellier

cdrick a écrit :


or again
removeAll: aCollection
aCollection == self
ifTrue: [self removeAll: aCollection copy]
ifFalse: [aCollection do: [:each | self remove: each]].

Or maybe, if aCollection == self, a warning could be raised ?

What do you think ?

Cédrick



aCollection == self is not the only case that can fail.
Imagine i pass a wrapper collection on self, like a MappedCollection.

When i will change self with remove:, i will also change the argument 
which is just a wrapper on self.


Hope my explanations are not too much confusing...

Nicolas

___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-19 Thread Klaus D. Witzel

On Tue, 19 Feb 2008 21:01:14 +0100, nicolas cellier wrote:


cdrick a écrit :

 or again
removeAll: aCollection
aCollection == self
ifTrue: [self removeAll: aCollection copy]
ifFalse: [aCollection do: [:each | self remove: each]].
 Or maybe, if aCollection == self, a warning could be raised ?
 What do you think ?
 Cédrick



aCollection == self is not the only case that can fail.
Imagine i pass a wrapper collection on self, like a MappedCollection.

When i will change self with remove:, i will also change the argument  
which is just a wrapper on self.


Hope my explanations are not too much confusing...


Let's see: Remove each element of aCollection from the receiver. If  
successful for each, answer aCollection. Otherwise create an error  
notification.


What mapping would *not* raise the error notification ;-)

BTW: MappedCollection does not exist in sq3.10-7159dev08.02.1.image ...

/Klaus


Nicolas



___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-19 Thread nicolas cellier

Klaus D. Witzel a écrit :

On Tue, 19 Feb 2008 21:01:14 +0100, nicolas cellier wrote:


cdrick a écrit :

 or again
removeAll: aCollection
aCollection == self
ifTrue: [self removeAll: aCollection copy]
ifFalse: [aCollection do: [:each | self remove: each]].
 Or maybe, if aCollection == self, a warning could be raised ?
 What do you think ?
 Cédrick



aCollection == self is not the only case that can fail.
Imagine i pass a wrapper collection on self, like a MappedCollection.

When i will change self with remove:, i will also change the argument 
which is just a wrapper on self.


Hope my explanations are not too much confusing...


Let's see: Remove each element of aCollection from the receiver. If 
successful for each, answer aCollection. Otherwise create an error 
notification.


What mapping would *not* raise the error notification ;-)



My interpretation of the comment was rather for the case when we try to 
remove an element which is not in the collection...

But OK, I take it as is...

However i can construct a test case that will not do what is expected 
and will not raise a notification, wanna bet?


| collec1 collec2 |
collec1 := OrderedCollection with: 'a' with: 'b' with: 'c'.
collec2 := MappedCollection collection: collec1 map: (1 to: 2).
collec2 only map first two elements 'a' and 'b'
collec1 removeAll: collec2.
this should remove 'a' and 'b' from collec1
self assert: collec1 first = 'c'
too bad... it's 'b'


BTW: MappedCollection does not exist in sq3.10-7159dev08.02.1.image ...



MappedCollection is just an example of possible wrapper collection, even 
if there is none in core images nowadays, that's a classical pattern 
which avoid copying for example.


MappedCOllection is in 3.9 image.


/Klaus


Nicolas


___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-19 Thread Klaus D. Witzel

On Tue, 19 Feb 2008 22:00:09 +0100, nicolas cellier wrote:


Klaus D. Witzel a écrit :

On Tue, 19 Feb 2008 21:01:14 +0100, nicolas cellier wrote:


cdrick a écrit :

 or again
removeAll: aCollection
aCollection == self
ifTrue: [self removeAll: aCollection copy]
ifFalse: [aCollection do: [:each | self remove:  
each]].

 Or maybe, if aCollection == self, a warning could be raised ?
 What do you think ?
 Cédrick



aCollection == self is not the only case that can fail.
Imagine i pass a wrapper collection on self, like a MappedCollection.

When i will change self with remove:, i will also change the argument  
which is just a wrapper on self.


Hope my explanations are not too much confusing...
 Let's see: Remove each element of aCollection from the receiver. If  
successful for each, answer aCollection. Otherwise create an error  
notification.

 What mapping would *not* raise the error notification ;-)



My interpretation of the comment was rather for the case when we try to  
remove an element which is not in the collection...

But OK, I take it as is...

However i can construct a test case that will not do what is expected  
and will not raise a notification, wanna bet?


No, just let the next people who attempt to bring MappedCollection back to  
Squeak  3.10 take care ;-)


BTW: do you have as much energy for fixing problems as you seem to have  
for imagine the currently non-existing ;-) This would help Squeak jump  
forward :)



| collec1 collec2 |
collec1 := OrderedCollection with: 'a' with: 'b' with: 'c'.
collec2 := MappedCollection collection: collec1 map: (1 to: 2).
collec2 only map first two elements 'a' and 'b'
collec1 removeAll: collec2.
this should remove 'a' and 'b' from collec1
self assert: collec1 first = 'c'
too bad... it's 'b'


BTW: MappedCollection does not exist in sq3.10-7159dev08.02.1.image ...



MappedCollection is just an example of possible wrapper collection, even  
if there is none in core images nowadays, that's a classical pattern  
which avoid copying for example.


MappedCOllection is in 3.9 image.


/Klaus


Nicolas



___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-19 Thread nicolas cellier

Klaus D. Witzel a écrit :

On Tue, 19 Feb 2008 22:00:09 +0100, nicolas cellier wrote:


However i can construct a test case that will not do what is expected 
and will not raise a notification, wanna bet?


No, just let the next people who attempt to bring MappedCollection back 
to Squeak  3.10 take care ;-)




As a user of collection wrappers, proposed patch just hit the alarm 
buzzer my nervous brain is equipped with.


But your position is wise. As long as a MappedCollection, lazy list or 
lazy collection (see these projects on SqueakSource) are all created 
under our control, we can manage...


BTW: do you have as much energy for fixing problems as you seem to have 
for imagine the currently non-existing ;-) This would help Squeak jump 
forward :)




That's precisely how i find and fix unknown bugs:
Imagine how i could break current implementation.

For this reason, most patches i proposed are minor.
Hope a few are usefull, since adopted in 3.8.1, 3.9 and 3.10.
Any minor contribution counts anyway :)

Nicolas

___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


[Newbies] Re: How to empty a collection?

2008-02-18 Thread Klaus D. Witzel

On Tue, 19 Feb 2008 02:35:34 +0100, Marcin Tustin wrote:


Isn't Cedrick's aida solution going to be significantly faster?


No, solutions based on #become: are the slowest possible. The VM has to  
sweep the whole memory, for all variables in the system, to find all  
references during #become:, like this


 | oop |
 oop := self someObject.
 [check+replace reference in variables of oop, then
  oop := oop nextObject.
  oop = 0
  ] whileFalse.

Now think that you (and every other user of your software ;-) have an  
.image with 2-4gigs every time you use #become: ...


If you want something faster, look at Bert's

 myCollection removeAllSuchThat: [:each | true]

and try to make it faster but keep still it correct (old elements must be  
nil'ed).


/Klaus


On Feb 19, 2008 1:02 AM, Ron Teitelbaum [EMAIL PROTECTED] wrote:


Hi Sophie,

aCollection copy do: [:anElement |
   aCollection remove: anElement
].

Why copy?  If you start removing items from the collection, increasing  
the

index will cause you to skip elements.

Ron

 -Original Message-
 From: itsme213

 I want to clear its contents (size goes back to zero), and can't seem  
to

 find something like #clear or #empty. Cannot use a new collection as
there
 are shared references to it.

 Thanks - Sophie


___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners




___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners