[Newbies] Re: How to empty a collection?
Klaus D. Witzel
klaus.witzel at cobss.com
Tue Feb 19 21:27:00 UTC 2008
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:
>>>> Or maybe, if aCollection == self, a warning could be raised ?
>>>> What do you think ?
>>> 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
>> 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
> | 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.
More information about the Beginners