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:. > |
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 |
2017-06-16 12:38 GMT+02:00 Stéphane Rollandin <[hidden email]>:
Does this even work ? Set does not preserve ordering. Item was added: + ----- Method: SequenceableCollection>> + 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]]! |
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]]! > > > |
2017-06-16 14:16 GMT+02:00 Levente Uzonyi <[hidden email]>: On Fri, 16 Jun 2017, Nicolai Hess wrote: Ah, of course :) Set is only used for the select.
|
Free forum by Nabble | Edit this page |