[Pharo-users] Set >> collect:thenDo:

Herby Vojčík herby at mailbox.sk
Sun Sep 8 09:32:10 EDT 2019


On 8. 9. 2019 14:28, Peter Kenny wrote:
> Two comments:
> First, the method comment for Collection>>collect:thenDo: is "Utility method
> to improve readability", which is exactly the same as for
> collect:thenSelect: and collect:thenReject:. This suggests that the
> *intention* of the method is not to introduce new behaviour, but simply to
> provide a shorthand for the version with parentheses. For other kinds of

I had that same impression.

> collection this is true; just the deduping makes Set different. If we want

I would be more defensive here and say that generic collection should 
have (collect:)do: implementation and only sequenceable collections have 
the optimized one (if it indeed is the case that it is a shotrhand for 
parenthesized one).

> the different behaviour, this should be indicated by method name and
> comment.
> Second, if we remove asSet from the second snippet, the output is exactly
> the same. It will be the same as long as the original collection has no
> duplicates. Somehow the effect is to ignore the asSet. It just smells wrong.
> 
> Peter Kenny

Herby

> Kasper Osterbye wrote
>> The first version:
>>
>> (#(1 2 3) asSet collect: #odd)
>> do: [ :each | Transcript show: each; cr ]
>>
>> is rather straight forward I believe, as collect: and do: has been around
>> forever (relatively speaking).
>>
>>
>> #(1 2 3) asSet collect: #odd
>> thenDo: [ :each | Transcript show: each; cr ]
>>
>>
>> On 8 September 2019 at 09.13.36, Richard Sargent (
> 
>> richard.sargent@
> 
>> ) wrote:
>>
>>   I am skeptical of one that relies on a specific implementation rather
>> than
>> a specific definition.
>>
>> I share your feeling. I am not sure where such a definition would come
>> from. In Squeak it is defined as:
>>
>> collect: collectBlock thenDo: doBlock
>>
>> ^ (self collect: collectBlock) do: doBlock
>>
>> In pharo as:
>>
>> collect: collectBlock thenDo: doBlock
>>
>> ^ self do: [ :each | doBlock value: (collectBlock value: each)]
>>
>> I might have called the method collect:eachDo:, but we each have slightly
>> different styles. What I like about the pharo version is that is a
>> shorthand for something which is not achieved by mere parentheses.
>>
>> Best,
>>
>> Kasper
> 
> 
> 
> 
> 
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
> 




More information about the Pharo-users mailing list