Skip to content

Commit

Permalink
Fixing #6373
Browse files Browse the repository at this point in the history
  • Loading branch information
olekscode committed May 29, 2020
1 parent dae367b commit fb8d4f7
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Collections-Abstract/Collection.class.st
Expand Up @@ -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' }
Expand Down

0 comments on commit fb8d4f7

Please sign in to comment.