typestar

Associations in Smalltalk

key -> value pairs, the atoms dictionaries are made of.

"key -> value builds an Association; dictionaries are made of them."
| job pairs |
job := 'broadside' -> 120.
Transcript show: job key, ' runs ', job value printString; cr.

pairs := OrderedCollection new.
pairs add: 'chapbook' -> 340; add: 'bookmark' -> 15.

pairs do: [:each |
    Transcript show: each key, ': ', each value printString; cr].

"A dictionary swallows associations directly."
Transcript show: (Dictionary withAll: pairs) size printString; cr.

How it works

  1. -> builds an Association with key and value.
  2. Pairs collect like any object.
  3. Dictionary withAll: swallows a collection of them.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
455
Tokens
72
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 3 in Order & identity, step 22 of 31 in Language basics.

← Previous Next →