typestar

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

  1. do:separatedBy: puts the separator only between elements.
  2. doWithIndex: numbers as it walks.
  3. reverseDo: starts from the far end.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Enumeration, step 17 of 31 in Language basics.

← Previous Next →