Skip to main content
Engineering LibreTexts

10.1: Two Sequences of Elements

  • Page ID
    36384
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    A good metaphor to understand a stream is the following: A stream can be represented as two sequences of elements: a past element sequence and a future element sequence. The stream is positioned between the two sequences. Understanding this model is important since all stream operations in Smalltalk rely on it. For this reason, most of the Stream classes are subclasses of PositionableStream. Figure \(\PageIndex{1}\) presents a stream which contains five characters. This stream is in its original position, i.e., there is no element in the past. You can go back to this position using the message reset.

    A stream positioned at its beginning.
    Figure \(\PageIndex{1}\): A stream positioned at its beginning.

    Reading an element conceptually means removing the first element of the future element sequence and putting it after the last element in the past element sequence. After having read one element using the message next, the state of your stream is that shown in Figure \(\PageIndex{2}\).

    The same stream after the execution of the method next.
    Figure \(\PageIndex{2}\): The same stream after the execution of the method next: the character a is “in the past” whereas b, c, d and e are “in the future”.

    Writing an element means replacing the first element of the future sequence by the new one and moving it to the past. Figure \(\PageIndex{3}\) shows the state of the same stream after having written an x using the message nextPut: anElement.

    The same stream after having written an x.
    Figure \(\PageIndex{3}\): The same stream after having written an x.

    This page titled 10.1: Two Sequences of Elements is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.