InjectionNull (was Re: [squeak-dev] Re: Collection>>sum implementation)

Jason Johnson jason.johnson.081 at gmail.com
Wed Aug 13 12:11:13 UTC 2008


On Tue, Aug 12, 2008 at 4:43 PM, Randal L. Schwartz
<merlyn at stonehenge.com> wrote:
>>>>>> "Randal" == Randal L Schwartz <merlyn at stonehenge.com> writes:
>
> Randal> Yes, that solves it for SequencableCollection, but not Collection.
> Randal> I think the trouble is how to solve it for the general case.
>
> This may have been brought up before, but it just occurred to me
> that we merely need an "InjectionNull" class that answers the first
> argument in response to any message send, through a simple DNU handler.
>
> Then we could implement #reduce: as:
>
> reduce: aBlock
>  self ifEmpty: [^ some exception ].
>  ^self inject: InjectionNull new into: aBlock.
>
> On the first round, the InjectionNull would answer the real
> first element, and then we've bootstrapped our loop at full speed
> for the remainder, regardless of whether it is sequenceable or not.

Interesting thought.

But the issue here is simply unordered collections, yes?  How do
unordered collections deal with #inject:into:?  Perhaps a possibility
would be to add a new method #inject:into:startingFrom: (or
#inject:into:skipping:) and implement the methods as:

inject: aVal into: aBlock
  ^ self inject: aVal into: aBlock startingFrom: 1

reduce: aBlock
  ^ self inject: self first into: aBlock startingFrom: 2

Of course unordered collections would have to specialize #reduce:
based on knowledge of their internal representation (i.e. how do we
pick an object and not traverse over it afterward).



More information about the Squeak-dev mailing list