Hi guys,
Collection defines #sum:, #detectSum: and #sumNumbers:, all of which accomplish the same (in principal) but with subtle differences:
#sum:
- uses #inject:into: with #anyOne as the injected element and will thus fail for empty collections
#detectSum:
- uses ���nextValue + sum��� instead of ���sum + nextValue��� which makes it a lot slower when dealing with large numbers (primitive fails and number conversion is necessary)
#sumNumbers:
- same as #sum but doesn���t fail for empty collections. Only good for numbers though.
Benchmarks:
[ 100 timesRepeat: [ (1 to: 1000000) sum: #yourself ] ] timeToRun 18062
[ 100 timesRepeat: [ (1 to: 1000000) detectSum: #yourself ] ] timeToRun 42391
[ 100 timesRepeat: [ (1 to: 1000000) sumNumbers: #yourself ] ] timeToRun 18096