Walking with style in Smalltalk
do:separatedBy:, doWithIndex:, reverseDo:.
"do:separatedBy: puts the separator only between elements."
| stations |
stations := #('compose' 'lock up' 'ink' 'pull').
stations do: [:step | Transcript show: step]
separatedBy: [Transcript show: ' -> '].
Transcript cr.
"doWithIndex: numbers as it walks."
stations doWithIndex: [:step :i |
Transcript show: i printString, '. ', step; cr].
"reverseDo: walks from the far end."
stations reverseDo: [:step | Transcript show: step, ' '].
Transcript cr.
How it works
do:separatedBy:puts the separator only between elements.doWithIndex:numbers as it walks.reverseDo:starts from the far end.
Keywords and builtins used here
TranscriptcrprintString
The run, in numbers
- Lines
- 15
- Characters to type
- 453
- Tokens
- 71
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 71 seconds.
Step 4 of 4 in Enumeration, step 17 of 31 in Language basics.