[Newbies] Re: How to empty a collection?

nicolas cellier ncellier at ifrance.com
Tue Feb 19 21:00:09 UTC 2008

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

More information about the Beginners mailing list