9.5: Collection Iterators
- Page ID
- 36382
\( \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}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)In Smalltalk loops and conditionals are simply messages sent to collections or other objects such as integers or blocks (see also Chapter 3). In addition to low-level messages such as to:do:
which evaluates a block with an argument ranging from an initial to a final number, the Smalltalk collection hierarchy offers various high-level iterators. Using such iterators will make your code more robust and compact.
Iterating (do:)
The method do:
is the basic collection iterator. It applies its argument (a block taking a single argument) to each element of the receiver. The following example prints all the strings contained in the receiver to the transcript.
#('bob' 'joe' 'toto') do: [:each | Transcript show: each; cr].
Variants. There are a lot of variants of do:
, such as do:without:
, doWithIndex:
and reverseDo:
: For the indexed collections (Array
, OrderedCollection
, SortedCollection
) the method doWithIndex:
also gives access to the current index. This method is related to to:do:
which is defined in class Number
.
#('bob' 'joe' 'toto') doWithIndex: [:each :i | (each = 'joe') ifTrue: [ ↑ i ] ] →
2
For ordered collections, reverseDo:
walks the collection in the reverse order.
The following code shows an interesting message: do:separatedBy:
which executes the second block only in between two elements.
res := ''. #('bob' 'joe' 'toto') do: [:e | res := res, e ] separatedBy: [res := res, '.']. res → 'bob.joe.toto'
Note that this code is not especially efficient since it creates intermediate strings and it would be better to use a write stream to buffer the result (see Chapter 10):
String streamContents: [:stream | #('bob' 'joe' 'toto') asStringOn: stream delimiter: '.' ] → 'bob.joe.toto'
Dictionaries. When the message do:
is sent to a dictionary, the elements taken into account are the values, not the associations. The proper methods to use are keysDo:
, valuesDo:
, and associationsDo:
, which iterate respectively on keys, values or associations.
colors := Dictionary newFrom: { #yellow --> Color yellow. #blue --> Color blue. #red --> Color red }. colors keysDo: [:key | Transcript show: key; cr]. "displays the keys" colors valuesDo: [:value | Transcript show: value;cr]. "displays the values" colors associationsDo: [:value | Transcript show: value;cr]. "displays the associations"
Collecting results (collect:)
If you want to process the elements of a collection and produce a new collection as a result, rather than using do:
, you are probably better off using collect:
, or one of the other iterator methods. Most of these can be found in the enumerating protocol of Collection
and its subclasses.
Imagine that we want a collection containing the doubles of the elements in another collection. Using the method do:
we must write the following:
double := OrderedCollection new. #(1 2 3 4 5 6) do: [:e | double add: 2 * e]. double → an OrderedCollection(2 4 6 8 10 12)
The method collect:
executes its argument block for each element and returns a new collection containing the results. Using collect:
instead, the code is much simpler:
#(1 2 3 4 5 6) collect: [:e | 2*e] → #(2 4 6 8 10 12)
The advantages of collect:
over do:
are even more dramatic in the following example, where we take a collection of integers and generate as a result a collection of absolute values of these integers:
aCol:= #(2 -3 4 -35 4 -11). result := aCol species new: aCol size. 1 to: aCol size do: [ :each | result at: each put: (aCol at: each) abs]. result → #(2 3 4 35 4 11)
Contrast the above with the much simpler following expression:
#(2 -3 4 -35 4 -11) collect: [:each|eachabs] → #(2 3 4 35 4 11)
A further advantage of the second solution is that it will also work for sets and bags.
Generally you should avoid using do:
, unless you want to send messages to each of the elements of a collection.
Note that sending the message collect:
returns the same kind of collection as the receiver. For this reason the following code fails. (A String
cannot hold integer values.)
'abc' collect: [:ea | ea asciiValue ] "error!"
Instead we must first convert the string to an Array
or an OrderedCollection
:
'abc' asArray collect: [:ea | ea asciiValue ] → #(97 98 99)
Actually collect:
is not guaranteed to return a collection of exactly the same class as the receiver, but only the same “species”. In the case of an Interval
, the species is actually an Array
!
(1to:5)collect:[:ea|ea*2] → #(2 4 6 8 10)
Selecting and rejecting elements
select:
returns the elements of the receiver that satisfy a particular condition:
(2 to: 20) select: [:each | each isPrime] → #(2 3 5 7 11 13 17 19)
reject:
does the opposite:
(2 to: 20) reject: [:each|eachisPrime] → #(4 6 8 9 10 12 14 15 16 18 20)
Identifying an element with detect:
The method detect:
returns the first element of the receiver that matches block argument.
'through' detect: [:each | each isVowel] → $o
The method detect:ifNone:
is a variant of the method detect:
. Its second block is evaluated when there is no element matching the block.
Smalltalk allClasses detect: [:each | '*java*' match: each asString] ifNone: [ nil ] → nil
Accumulating results with inject:into:
Functional programming languages often provide a higher-order function called fold or reduce to accumulate a result by applying some binary operator iteratively over all elements of a collection. In Squeak this is done by Collection»inject:into:.
The first argument is an initial value, and the second argument is a two-argument block which is applied to the result this far, and each element in turn.
A trivial application of inject:into:
is to produce the sum of a collection of numbers. Following Gauss, in Squeak we could write this expression to sum the first 100 integers:
(1 to: 100) inject: 0 into: [:sum :each | sum + each ] → 5050
Another example is the following one-argument block which computes factorials:
factorial := [:n | (1 to: n) inject: 1 into: [:product :each | product * each ] ]. factorial value: 10 → 3628800
Other messages
count: The message count:
returns the number of elements satisfying a condition. The condition is represented as a boolean block.
Smalltalk allClasses count: [:each | 'Collection*' match: each asString ] → 2
includes: The message includes:
checks whether the argument is contained in the collection.
colors := {Color white . Color yellow. Color red . Color blue . Color orange}. colors includes: Color blue. → true
anySatisfy: The message anySatisfy:
answers true if at least one element of the collection satisfies the condition represented by the argument.
colors anySatisfy: [:c | c red > 0.5] → true