9.7: Chapter Summary
- Page ID
- 39185
\( \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}}\)
The Smalltalk collection hierarchy provides a common vocabulary for uniformly manipulating a variety of different kinds of collections.
- A key distinction is between
SequenceableCollection
s, which maintain their elements in a given order,Dictionary
and its subclasses, which maintain key-to-value associations, andSet
s andBag
s, which are unordered. - You can convert most collections to another kind of collection by sending them the messages
asArray
,asOrderedCollection
etc.. - To sort a collection, send it the message
asSortedCollection
. - Literal Arrays are created with the special syntax #( ... ). Dynamic Arrays are created with the syntax { ... }.
- A
Dictionary
compares keys by equality. It is most useful when keys are instances ofString
. AnIdentityDictionary
instead uses object identity to compare keys. It is more suitable whenSymbol
s are used as keys, or when mapping object references to values. String
s also understand the usual collection messages. In addition, aString
supports a simple form of pattern-matching. For more advanced application, look instead at the RegEx package.- The basic iteration message is
do:
. It is useful for imperative code, such as modifying each element of a collection, or sending each element a message. - Instead of using
do:
, it is more common to usecollect:
,select:
,reject:
,includes:
,inject:into:
and other higher-level messages to process collections in a uniform way. - Never remove an element from a collection you are iterating over. If you must modify it, iterate over a copy instead.
- If you override =, remember to override
hash
as well!