Collection statistics in Smalltalk
max, min, average, count: — collections answer themselves.
"Collections answer their own statistics."
| runs |
runs := #(120 340 85 500 210).
Transcript show: 'Max: ', runs max printString; cr.
Transcript show: 'Min: ', runs min printString; cr.
Transcript show: 'Sum: ', (runs inject: 0 into: [:a :b | a + b])
printString; cr.
Transcript show: 'Average: ', runs average printString; cr.
Transcript show: (runs count: [:n | n > 100]) printString,
' runs over 100'; cr.
Transcript show: (runs allSatisfy: [:n | n > 50]) printString; cr.
Transcript show: (runs anySatisfy: [:n | n > 450]) printString; cr.
How it works
maxandminneed no loop.averageanswers an exact fraction.count:,allSatisfy:,anySatisfy:take predicates.
Keywords and builtins used here
TranscriptaveragecrmaxminprintString
The run, in numbers
- Lines
- 14
- Characters to type
- 546
- Tokens
- 124
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 124 seconds.
Step 3 of 4 in Enumeration, step 16 of 31 in Language basics.