Strings and symbols in Smalltalk
Strings are collections; symbols are interned names.
"Strings are collections; symbols are unique interned names."
| title |
title := 'letterpress broadside'.
Transcript show: title asUppercase; cr.
Transcript show: (title copyReplaceAll: 'broad' with: 'bed'); cr.
Transcript show: (title indexOf: $b) printString; cr.
Transcript show: title reversed; cr.
"A symbol equals itself everywhere; strings only by content."
Transcript show: (#pressRun == #pressRun) printString; cr.
Transcript show: ('run' = 'run') printString; cr.
Transcript show: #broadside printString; cr.
How it works
copyReplaceAll:with:rewrites;reversedflips.indexOf: $bfinds a character's position.- Symbols compare with
==— one object per name.
Keywords and builtins used here
TranscriptasUppercasecrprintStringreversed
The run, in numbers
- Lines
- 13
- Characters to type
- 520
- Tokens
- 75
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 75 seconds.
Step 1 of 3 in Strings, step 18 of 31 in Language basics.