The Trunk: Collections-eem.756.mcz

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

Re: The Trunk: Collections-eem.756.mcz

Francisco Garau-2
How about #addWhenAbsent: ?

- Francisco


On 15 Jun 2017, at 23:58, Chris Muller <[hidden email]> wrote:

>> And, I didn't go with #ifMissingAdd: because it confuses the return value with the linguistic semantics -- e.g., if it returns true, does that mean it was "absent" or that it was added?  forcing me to look at the method implementation again anyway.
>
> I think I confused this with a different one.  ifMissingAdd:
> actually seems fine in terms of the return value semantics..  "true"
> would mean, "yes", it was missing and added.
>
> From there, saying we want to stay with the "Absent" terminology in the
> API seems reasonable.
>
> +1 for ifAbsentAdd:.
>

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Collections-eem.756.mcz

Stéphane Rollandin
In reply to this post by Eliot Miranda-2
>
> Stéphane, can you live with ifMissingAdd: ?  Chris?
>

Sure. Now "missing" seems to imply that something went wrong and an
element is not there while it should (which is why I would prefer
#ifAbsentAdd:).

Plus, the name does not tell that a Boolean is returned (this apply to
#ifAbsentAdd: too).

So what about #ensurePresenceOf: ?

I would expect this to return true if the element was *not* added, false
otherwise, though.


Stef

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Collections-eem.756.mcz

Nicolai Hess-3-2


2017-06-16 12:38 GMT+02:00 Stéphane Rollandin <[hidden email]>:

Stéphane, can you live with ifMissingAdd: ?  Chris?


Sure. Now "missing" seems to imply that something went wrong and an element is not there while it should (which is why I would prefer #ifAbsentAdd:).

Plus, the name does not tell that a Boolean is returned (this apply to #ifAbsentAdd: too).

So what about #ensurePresenceOf: ?

I would expect this to return true if the element was *not* added, false otherwise, though.


Stef


Does this even work ?
Set does not preserve ordering.

Item was added:
+ ----- Method: SequenceableCollection>>withoutDuplicates (in category 'copying') -----
+ withoutDuplicates
+       "Answer a copy of the receiver that preserves order but eliminates any duplicates."
+       | seen |
+       seen := Set new: self size.
+       ^self select: [:each|
+                                 (seen includes: each)
+                                       ifTrue: [false]
+                                       ifFalse: [seen add: each. true]]!



Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Collections-eem.756.mcz

Levente Uzonyi
On Fri, 16 Jun 2017, Nicolai Hess wrote:

> Does this even work ?
> Set does not preserve ordering.

The receiver is not a Set but a SequenceableCollection, so yes, it does
work:

  | set |
  set := Set new.
  #(1 1 2 1 2 3 1 2 3 4) select: [ :each | set addNewElement: each ]

  "==> #(1 2 3 4)"

Levente

>
> Item was added:
> + ----- Method: SequenceableCollection>>withoutDuplicates (in category 'copying') -----
> + withoutDuplicates
> +       "Answer a copy of the receiver that preserves order but eliminates any duplicates."
> +       | seen |
> +       seen := Set new: self size.
> +       ^self select: [:each|
> +                                 (seen includes: each)
> +                                       ifTrue: [false]
> +                                       ifFalse: [seen add: each. true]]!
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Collections-eem.756.mcz

Nicolai Hess-3-2


2017-06-16 14:16 GMT+02:00 Levente Uzonyi <[hidden email]>:
On Fri, 16 Jun 2017, Nicolai Hess wrote:

Does this even work ?
Set does not preserve ordering.

The receiver is not a Set but a SequenceableCollection, so yes, it does work:

        | set |
        set := Set new.
        #(1 1 2 1 2 3 1 2 3 4) select: [ :each | set addNewElement: each ]

        "==> #(1 2 3 4)"

Ah, of course :)
Set is only used for the select.
 

Levente



Item was added:
+ ----- Method: SequenceableCollection>>withoutDuplicates (in category 'copying') -----
+ withoutDuplicates
+       "Answer a copy of the receiver that preserves order but eliminates any duplicates."
+       | seen |
+       seen := Set new: self size.
+       ^self select: [:each|
+                                 (seen includes: each)
+                                       ifTrue: [false]
+                                       ifFalse: [seen add: each. true]]!








12