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!