diff --git a/src/Collections-Abstract/Collection.class.st b/src/Collections-Abstract/Collection.class.st index 018c0d88a89..bb3aaf57baf 100644 --- a/src/Collections-Abstract/Collection.class.st +++ b/src/Collections-Abstract/Collection.class.st @@ -1253,15 +1253,30 @@ Collection >> storeOn: aStream [ { #category : #'math functions' } Collection >> sum: aBlock [ + "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." + ^ self sum: aBlock ifEmpty: [ 0 ] +] + +{ #category : #'math functions' } +Collection >> sum: aBlock ifEmpty: anEmptySumBlock [ "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 := aBlock value: self anyOne. - sum := self inject: sample into: [ :previousValue :each | previousValue + (aBlock value: each) ]. - ^ sum - sample + + ^ self + ifNotEmpty: [ + sample := aBlock value: self anyOne. + sum := self inject: sample into: [ :previousValue :each | + previousValue + (aBlock value: each) ]. + sum - sample ] + ifEmpty: anEmptySumBlock. ] { #category : #'math functions' }