typestar

Arrays and intervals in Smalltalk

Literal #() arrays, lazy to: ranges.

"Literal arrays with #(), intervals with to:, both collections."
| picas evens |
picas := #(6 12 18 24 30).
Transcript show: picas first printString, ' to ',
    picas last printString; cr.

"An Interval is a lazy range that answers like a collection."
evens := (2 to: 12 by: 2).
Transcript show: evens asArray printString; cr.
Transcript show: (evens includes: 8) printString; cr.

"with:do: walks two collections in step."
#('title' 'body' 'folio') with: #(24 10 8) do: [:part :size |
    Transcript show: part, ' at ', size printString, 'pt'; cr].

How it works

  1. #(6 12 18) is a literal array of numbers.
  2. (2 to: 12 by: 2) answers like a collection without being one.
  3. with:do: walks two collections in step.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
542
Tokens
91
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 91 seconds.

Type this snippet

Step 4 of 4 in Collections, step 13 of 31 in Language basics.

← Previous Next →