[squeak-dev] Re: Collection>>sum implementation

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


>>>>> "Jason" == Jason Johnson <jason.johnson.081 at gmail.com> writes:

Jason> Here is Lukas' implementation of SequencableCollection>>reduce:

Jason> reduce: aBlock
Jason> 	| result |
Jason> 	result := self first.
Jason> 	2 to: self size do: [ :index |
Jason> 		result := aBlock
Jason> 			value: result
Jason> 			value: (self at: index) ].
Jason> 	^ result

Jason> You see?  We take an element from the receiver as we must for the
Jason> reasons mentioned in the mantis report, *but we don't double process
Jason> it and we don't have to take it back out again*.  Simple and elegant
Jason> and also handles the case of an empty list with a sensible error
Jason> (#first fails with a bounds error).

Yes, that solves it for SequencableCollection, but not Collection.
I think the trouble is how to solve it for the general case.

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