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

Randal L. Schwartz merlyn at stonehenge.com
Tue Aug 12 14:43:49 UTC 2008


>>>>> "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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion



More information about the Squeak-dev mailing list