[squeak-dev] Re: #collect: on strings
Klaus D. Witzel
klaus.witzel at cobss.com
Thu Jun 26 06:11:36 UTC 2008
On Thu, 26 Jun 2008 00:18:08 +0200, Nicolas wrote:
> 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.
Sure. BTW #couldStore: can be replaced by the VM's unwillingness to store,
say of Damien's boolean into an instance of String--and so wouldn't cost
anything besides the handler and its work.
> 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.
Except when you don't want/can make a copy/#asArray of the source
collection.
> 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.
And by code duplication, #collect:into: could do the job ;)
> Nicolas
>
More information about the Squeak-dev
mailing list
|