Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sum
- "sum up all elements.
- This is implemented using a variant of the normal inject:into: pattern.
- The reason for this is that it is not known whether we are dealing with number
- (i.e. if 0 is a good initial value for the sum).
- Consider a collection of measurement or physical objects, 0 would be the unitless
- value and would not be appropriate to add with the unit-ed objects."
- | sum sample |
- sample := self anElement.
- sum := self inject: sample into: [:accum :each | accum + each].
- ^ sum - sample.
- "
- TestCase should: [ { } sum ] raise:Error.
- TestCase should: [ '' sum ] raise:Error.
- TestCase assert: ( { 1 } sum = 1 ).
- TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ).
- TestCase assert: ( (1 to:10) sum = 55 ).
- TestCase assert: ( 'abc' asByteArray sum = 294 ).
- TestCase assert: ( { 10 +/- 2.
- 20 +/- 4.
- 100 +/- 10 } sum = (130 +/- 16) ).
- TestCase assert: ( { (1 / 9).
- (1 / 7).
- } sum = (16 / 63) ).
- "
Add Comment
Please, Sign In to add comment