• Home
  • Textbooks
  • A little Smalltalk
  • Generators

A little Smalltalk

Timothy Budd

Chapter 8

Generators - all with Video Answers

Educators


Chapter Questions

Problem 1

Rewrite the primes program from the section on filters to use instances of AbstractGenerator in place of FilterFactor.

Check back soon!

Problem 2

Consider the three variables formed in the following manner:
$a \leftarrow 1$ to: 3
$b \leftarrow$ AbstractGenerator new ; firstBlock: [ a ] nextBlock: [ nit ]
$c \leftarrow$ AbstractGenerator new ; firstBlock: [ a first ] nextBlock: [ a
next ]
$a \leftarrow 1$ to: 3
$b \leftarrow$ AbstractGenerator new ; firstBlock: [ a ] nextBlock: [ nil ]
$\mathrm{c} \leftarrow$ AbstractGenerator new ; firstBlock: [ a first ] nextBlock: [ a
next ]
Discuss the similarities and differences among $\mathrm{a}, \mathrm{b}$ and $\mathrm{c}$. In particular how does each of them react to the messages first, next and do:?

Check back soon!
03:15

Problem 3

Look at the method inherited for select: and reject: in class Collection. Explain why it is necessary to implement only one of these messages in class Generator.

Jennifer Stoner
Jennifer Stoner
Numerade Educator
View

Problem 4

An alternative to the first next paradigm described in this chapter is the following pair of messages:
reset Reset the generator to the start of the sequence, but do not produce a value.
next Return the next value from the sequence (which may be the first value if the last message sent to the receiver was reset).
Similarly, instead of returning nil to indicate the end of sequence, a message atEnd could be implemented by each generator. This message would return true if no more elements could be returned by the generator, and false otherwise. Discuss the advantages and disadvantages of these techniques. Support your opinions by rewriting some of the generators described in this chapter so that they use these alternatives.

Victor Salazar
Victor Salazar
Numerade Educator
View

Problem 5

Explain why the following does not produce the expected result:
$$
\begin{aligned}
& a \leftarrow 1 \text { to } 9 \\
& b \leftarrow \text { a shuffle: [:x }: y \mid \text { true] with: } a
\end{aligned}
$$
Is this a problem inherent with the generators technique described in this chapter? How might it be overcome?

Alexander Cheng
Alexander Cheng
Numerade Educator

Problem 6

Why does the following not succeed in producing a sequence of length 4 ?
$$
\mathrm{i} \leftarrow \text { (Primes new) select: }[: \mathrm{x} \mid \mathrm{x}<10]
$$
Show how to implement methods for the following messages:
while: The argument must be a one-parameter block returning a boolean value. Return values from the underlying generator as long as the block evaluates to true. Terminate when the underlying generator is exhausted or when a value is found for which the block is not true.
until: Like while: The argument must be one-parameter block returning a boolean value. Produce values from the underlying generator until the block returns true, or until the generator is exhausted.
Is it necessary to implement both of these in terms of AbstractGenerator? Show how, if a method exists for either one, the other can be implemented in terms of it.

Check back soon!
00:19

Problem 7

The following method in class Generator produces a sequence consisting of the first $n$ elements of the underlying generator, where $n$ is the integer argument.
first: limit $\quad$ | counter testBlock $\mid$
testBlock $\leftarrow[: x \mid$ counter $\leftarrow$ counter +1.
(counter $>$ limit)
iffalse: $[x]]$.
$\uparrow$ AbstractGenerator new $;$
firstBlock: [ counter $\leftarrow 0$.
testBlock value: self first ]
nextBlock: [ testBlock value: self next ]
first: limit $\quad \mid$ counter testBlock $\mid$
testBlock $\leftarrow[: x \mid$ counter $\leftarrow$ counter +1.
(counter $>$ limit)
iffalse: $[x]]$.
$\uparrow$ AbstractGenerator new ;
firstBlock: [ counter $\leftarrow 0$.
testBlock value: self first ]
nextBlock: [ testBlock value: self next ]
Show how to implement a method for the message from:to; that can be used to extract any contiguous set of values from the underlying generator, specified in terms of indices. Extend this to return any set of values for which the indices form an arithmetic progression.

Amy Jiang
Amy Jiang
Numerade Educator

Problem 8

Let us say a generated sequence is enumerable if every value in the sequence will eventually be produced, assuming the generator is queried long enough. Consider the message atEachGenerate; under which of the following conditions is the resulting sequence enumerable?
a) The underlying generator is finite; each of the intermediate generators is finite.
b) The underlying generator is finite; however, some of the intermediate generators are infinite.
c) The underlying generator is infinite; however, all of the intermediate generators are finite.
d) The underlying generator is infinite, and some of the intermediate generators are also infinite.

Check back soon!
View

Problem 9

Consider the following scheme for assigning indices to the values produced as a result of the message atEachGenerate: Let $(1,1)$ be the index of the first value produced by the first intermediate generator; $(1,2)$, the index of the second value produced by the first generator; $(2,1)$, the first value produced by the second generator, and so on. If the first intermediate generator produces $\mathrm{n}$ values and the second intermediate generator $m$ values, then the sequence of indices produced can be represented as follows:
$$(1,1)(1,2) \ldots(1, n)(2,1)(2,2) \ldots(2, m)(3,1) \ldots$$
Construct a method which produces the same values but in the following dovetailed sequence:
$$(1,1)(1,2)(2,1)(1,3)(2,2)(3,1)(1,4)(2,3)(3,2)(4,1) \ldots$$
Note that this involves keeping a list of intermediate generators, rather than just the single intermediate generator required for atEachGenerate: Under which of the conditions given in problem 6 is the resulting sequence enumerable?

Victor Salazar
Victor Salazar
Numerade Educator