Issue 3235 in pharo: better bag sum

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue 3235 in pharo: better bag sum

pharo
Status: FixedWaitingToBePharoed
Owner: stephane.ducasse
Labels: Type-Squeak Difficulty-Easy

New issue 3235 by stephane.ducasse: better bag sum
http://code.google.com/p/pharo/issues/detail?id=3235

- reimplemented Bag >> #sum to fix http://bugs.squeak.org/view.php?id=6560

=============== Diff against Collections-ul.402 ===============

Item was changed:
  ----- Method: Bag>>sum (in category 'math functions') -----
  sum
        "Faster than the superclass implementation when you hold many  
instances of the same value (which you probably do, otherwise you wouldn't  
be using a Bag)."
+
+       | sum first |
+       first := true.
+       contents keysAndValuesDo: [ :value :count |
+               first
+                       ifTrue: [ sum := value * count. first := false ]
+                       ifFalse: [ sum := sum + (value * count) ] ].
+       first ifTrue: [ self errorEmptyCollection ].
-       | sum |
-       sum := 0.
-       contents keysAndValuesDo: [:value :count | sum := sum + (value *  
count)].
        ^sum!


Reply | Threaded
Open this post in threaded view
|

Re: Issue 3235 in pharo: better bag sum

pharo
Updates:
        Status: Fixed

Comment #1 on issue 3235 by stephane.ducasse: better bag sum
http://code.google.com/p/pharo/issues/detail?id=3235

(No comment was entered for this change.)


Reply | Threaded
Open this post in threaded view
|

Re: Issue 3235 in pharo: better bag sum

pharo
Updates:
        Status: FixedWaitingToBePharoed

Comment #2 on issue 3235 by stephane.ducasse: better bag sum
http://code.google.com/p/pharo/issues/detail?id=3235

This is strange in pharo there is no sum in Bag just in Collection

Collection>>sum
        "This is implemented using a variant of the normal inject:into: pattern.
        The reason for this is that it is not known whether we're in the normal
        number line, i.e. whether 0 is a good initial value for the sum.
        Consider a collection of measurement objects, 0 would be the unitless
        value and would not be appropriate to add with the unit-ed objects."
        | sum sample |
        sample := self anyOne.
        sum := self inject: sample into: [:accum :each | accum + each].
        ^ sum - sample