typestar

WriteStream in Smalltalk

Build a string without repeated concatenation.

"A WriteStream builds a string without repeated concatenation."
| report |
report := WriteStream on: String new.
report
    nextPutAll: 'DAILY DOCKET';
    cr;
    nextPutAll: '3 jobs, ';
    print: 545;
    nextPutAll: ' sheets'.

Transcript show: report contents; cr.

"streamContents: wraps the same pattern in one expression."
Transcript show: (String streamContents: [:s |
    #('set' 'ink' 'pull') do: [:step | s nextPutAll: step]
        separatedBy: [s nextPutAll: ' / ']]); cr.

How it works

  1. WriteStream on: String new opens the builder.
  2. nextPutAll: appends; print: formats numbers in.
  3. streamContents: wraps the pattern in one expression.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
454
Tokens
78
Three-star pace
55 tpm

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

Type this snippet

Step 1 of 2 in Streams, step 24 of 31 in Language basics.

← Previous Next →