[squeak-dev] Re: #collect: on strings

nicolas cellier ncellier at ifrance.com
Wed Jun 25 22:18:08 UTC 2008


Klaus D. Witzel a écrit :
> On Wed, 25 Jun 2008 22:37:52 +0200, Damien Cassou wrote:
> 
>> On Wed, Jun 25, 2008 at 4:59 PM, Klaus D. Witzel
> ...
>>>>> But what do *you* expect to happen with your original expression, 
>>>>> mind to share?
>>>>
>>>> A collection of boleans.
>>>
>>> Well, let's look at #collect: it has a block as argument. Because of 
>>> this
>>> object orientedness, the implementor of #collect: cannot know what 
>>> sort of
>>> values the block will return in the future, no?
>>>
>>> How about this, let #collect: always collect into 
>>> anOrderedCollection, then it can eventually
>>>
>>>  ^ anOrderedCollection coercedWith: self
>>
>> What does #coercedWith: mean? I would agree if it is something like:
>> "try to make a collection of the same species as the parameter with
>> the content of the receiver and if it is not possible, return the
>> receiver"
> 
> Right. It would perhaps ask (argument #couldStore: anElement) and, 
> ifFalse, return a #copyFrom:to: of its array (Array should be the 
> default, IHMO). And if all pass then
> 

Another pattern: rather than testing #couldStore: as a precondition 
inside the loop, do handle the Exception outside the loop.
But write the handler so as to not iterate twice on already iterated 
elements (the block might have side effects).

>  ^ argument species withAll: self
> 
> For sure this all could also be moved right into the #collect: method, 
> might perform a bit faster. But anyways, things seem to have such price 
> tag.
> 

Simplicity of workaround: 'abc' asArray collect: [...] will be hard to beat.
It's probably not worth trying to save the cost of additional asArray 
iteration, given the complexification and de-optimization of code 
implied by genericity.

This makes my vote go to a statu quo.

Nicolas




More information about the Squeak-dev mailing list