Andreas Raab uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ar.357.mcz==================== Summary ====================
Name: Collections-ar.357
Author: ar
Time: 4 May 2010, 8:54:01.66 am
UUID: f4ac1d9d-b397-5e4d-8c27-8b047aea10ad
Ancestors: Collections-ul.356
Fix
http://bugs.squeak.org/view.php?id=6560Provide an implementation of Collection>>reduce: and implement sum in terms of #reduce:.
=============== Diff against Collections-ul.356 ===============
Item was added:
+ ----- Method: Collection>>reduce: (in category 'enumerating') -----
+ reduce: binaryBlock
+ "Apply the argument, binaryBlock cumulatively to the elements of the receiver.
+ For sequenceable collections the elements will be used in order, for unordered
+ collections the order is unspecified."
+
+ | first nextValue |
+ self emptyCheck.
+ first := true.
+ self do:[:each|
+ first ifTrue:[nextValue := each. first := false]
+ ifFalse:[nextValue := binaryBlock value: nextValue value: each]].
+ ^nextValue!
Item was changed:
----- Method: Collection>>sum (in category 'math functions') -----
sum
+ "Compute the sum of all the elements in the receiver"
+
+ ^self reduce:[:a :b| a + b]!
- "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!