Collection >> #removeIdentical:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Collection >> #removeIdentical:

Leandro Caniglia
What's the best way to implement the method of the subject?
 
self removeAllSuchThat: [:x | x == anObject] won't work.
 
/Leandro


Reply | Threaded
Open this post in threaded view
|

Re: Collection >> #removeIdentical:

Diego Fernández
It's really strange, these two works in my image (Squeak-3.9-7058):

(Set withAll: #(a b c a) ) removeAllSuchThat: [:x | x == #a ].
(OrderedCollection withAll: #(a b c a) )
      removeAllSuchThat: [:x | x == #a ].

May be you can get the same effect with something like this:
(the worst case it's O(n^2) like removeAllSuchThat: in Collection)

| s |
s := Set withAll: #(a b c a).
s removeAll: (s select: [:x | x == #a ]).

It's really necesary to keep the same Collection instance (why not
simply reject:) ?

On 9/19/06, Leandro Caniglia <[hidden email]> wrote:

>
>
> What's the best way to implement the method of the subject?
>
> self removeAllSuchThat: [:x | x == anObject] won't work.
>
> /Leandro
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Collection >> #removeIdentical:

Leandro Caniglia
Hi Diego.

My claim was that #removeAllSuchThat: won't work in the general case. The
reason is that it uses #remove:. In consequence, the first element which is
= to the argument gets removed. However, #removeIdentical: was intended to
remove the argument, and not another object that equals it. Note also that
your examples work because your sets have only one #a, so there is no
ambiguity between = and ==.

Note also that I asked this question as an "exercise" and not because of any
practical needs.

/Leandro


----- Original Message -----
From: "Diego Fernandez" <[hidden email]>
To: "The general-purpose Squeak developers list"
<[hidden email]>
Sent: Wednesday, September 20, 2006 11:02 AM
Subject: Re: Collection >> #removeIdentical:


> It's really strange, these two works in my image (Squeak-3.9-7058):
>
> (Set withAll: #(a b c a) ) removeAllSuchThat: [:x | x == #a ].
> (OrderedCollection withAll: #(a b c a) )
>      removeAllSuchThat: [:x | x == #a ].
>
> May be you can get the same effect with something like this:
> (the worst case it's O(n^2) like removeAllSuchThat: in Collection)
>
> | s |
> s := Set withAll: #(a b c a).
> s removeAll: (s select: [:x | x == #a ]).
>
> It's really necesary to keep the same Collection instance (why not
> simply reject:) ?
>
> On 9/19/06, Leandro Caniglia <[hidden email]> wrote:
>>
>>
>> What's the best way to implement the method of the subject?
>>
>> self removeAllSuchThat: [:x | x == anObject] won't work.
>>
>> /Leandro
>>
>>
>>
>>
>