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

Jason Johnson jason.johnson.081 at gmail.com
Tue Aug 12 06:55:47 UTC 2008


On Mon, Aug 11, 2008 at 10:15 PM, Jerome Peace
<peace_the_dreamer at yahoo.com> wrote:
> [squeak-dev] Collection>>sum implementation Jason Johnson jason.johnson.081
> at gmail.com Mon Aug 11 11:57:54 UTC 2008 Hi Jason, I remember debugging an
> instance of this problem a while back. Ah found it:
> http://bugs.squeak.org/view.php?id=6560 [BUG][FIX] "{Color red. Color green.
> Color blue} sum" can't return "Color white" The solution was to inject the
> zero element then sum the whole list. How do you get a zero element when you
> don't know what you are dealing with? You let it tell you: any :=
> myCollection anyone . zeroElement := any - any . answer := myCollection
> inject: zeroElement into: [ :sum :each | sum + each ] . The mantis report
> needs to be harvested. Hth, Yours in curiosity and service, --Jerome Peace
> *** Jason>Hello all, Jason> Jason>In my explorations today I happened on the
> implementation of the #sum Jason>method: Jason> Jason>Collection>>sum Jason>
> "This is implemented using a variant of the normal inject:into: pattern.
> Jason> The reason for this is that it is not known whether we're in the
> normal Jason> number line, i.e. whether 0 is a good initial value for the
> sum. Jason> Consider a collection of measurement objects, 0 would be the
> unitless Jason> value and would not be appropriate to add with the unit-ed
> objects." Jason> | sum sample | Jason> sample _ self anyOne. Jason> sum _
> self inject: sample into: [:accum :each | accum + each]. Jason> ^ sum -
> sample Jason> Jason> Jason>Personally, I view the code in the core image to,
> among other things, Jason>serve the role of showing new Smalltalk
> programmers good ways of using Jason>the language. This is exactly the kind
> of thing I would *not* want Jason>people to see. Jason> Jason>The obvious
> issue here is that #inject:into: doesn't fit this case Jason>very well. But
> rather then go to these kinds of steps a much better Jason>solution would be
> to simply notice that a specialized variation of Jason>#inject:into: (that
> is in fact quite common) is needed here. Namely Jason>a reduction method
> that just uses the first element as the starting Jason>value, conceptually:
> Jason> Jason>Collection>>reduce: aBlock Jason> ^ self allButFirst inject:
> self first into: aBlock Jason> Jason>As it turns out there exists a more
> efficient implementation of this Jason>method in Lukas' Magritte-Model
> package. I would propose his method Jason>be promoted to a core method so
> #sum could be rewritten as: Jason> Jason>sum Jason> "sum the reciever"
> Jason> ^ self reduce: [:accum :each| accum + each ] Jason> Jason>Thanks,
> Jason>Jason ***

The formatting of your message got really trashed in gmail so I can't
be sure I understand your response, but if it is the way I read it
then I think you didn't read my message.

It is absolutely not necessary to do the inefficient:  (1) insert one
element of the receiver, (2) sum the receiver (NOTE: one element is
processed twice), (3) take the "sample" back out again.

For me this situation brings up a typical Smalltalk situation where
the code is literally telling us it that is requires a special case.
Namely a method that preforms a reduction on the receiver, but without
requiring an "initial value" to inject.  And as is often the case,
once we have this method we find it is generally useful (as evidence
by the fact that Lukas make his own).



More information about the Squeak-dev mailing list