ReadStream in Smalltalk
Walk a collection with a cursor.
"A ReadStream walks a collection with a cursor."
| feed |
feed := ReadStream on: #('frisket' 'tympan' 'platen' 'chase').
Transcript show: 'First out: ', feed next; cr.
Transcript show: 'Then: ', feed next; cr.
Transcript show: 'Peek: ', feed peek; cr.
Transcript show: 'Still next: ', feed next; cr.
[feed atEnd] whileFalse: [
Transcript show: 'Draining: ', feed next; cr].
Transcript show: 'Empty? ', feed atEnd printString; cr.
How it works
nextconsumes one element;peeklooks without taking.atEndguards the draining loop.- Streams unify collections and files behind one protocol.
Keywords and builtins used here
ReadStreamTranscriptatEndcrnextpeekprintString
The run, in numbers
- Lines
- 12
- Characters to type
- 431
- Tokens
- 77
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 77 seconds.
Step 2 of 2 in Streams, step 25 of 31 in Language basics.