11.7: Chapter Summary
- Page ID
- 43129
\( \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 collection hierarchy provides a common vocabulary for uniformly manipulating a variety of different kinds of collections.
- A key distinction is between
SequenceableCollections
, which maintain their elements in a given order,Dictionary
and its subclasses, which maintain key-to-value associations, andSets
andBags
, 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
. #( ... )
creates arrays containing only literal objects (i.e., objects created without sending messages).{ ... }
creates dynamic arrays using a compact form.- 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 whenSymbols
are used as keys, or when mapping object references to values. Strings
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!