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
WriteStream on: String newopens the builder.nextPutAll:appends;print:formats numbers in.streamContents:wraps the pattern in one expression.
Keywords and builtins used here
StringTranscriptWriteStreamcontentscrnewreport
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.
Step 1 of 2 in Streams, step 24 of 31 in Language basics.