typestar

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

  1. next consumes one element; peek looks without taking.
  2. atEnd guards the draining loop.
  3. Streams unify collections and files behind one protocol.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 2 in Streams, step 25 of 31 in Language basics.

← Previous Next →