Iterators, etc.
- Apply a block to each element of a collection:
aSet do: [| :el | el printLine]
vec do: [| :e. :k. | e position: k]
- Mapping operators:
coll mapBy: [| :e | e squared ]
- Filtering:
iList filterBy: [| :n | n even]
- Convertors:
aSet asVector
vec asSet
|
Iterators, filters, mappers, convertors
There are many operations common to all collections. Perhaps the most important
are those that can apply a block to each element of a collection. The basic iterator is
do:. It takes a block (or anything that responds to value:With:) as argument, and
sends it value:With: with each element and key of the collection. (For unordered
collections, the key has little meaning. Note that blocks in Self can take
fewer arguments that supplied, so the key can be ignored.) Browse traits collection
and traits indexable for the complete set of iterators.
The elements of a collection can be mapped using a block as the mapping function,
with the messages mapBy:, copyMappedBy: and mapBy:Into:.
Collections can have elements filtered from them using a block as a predicate; the
messages are filterBy:, copyFilteredBy: and filterBy:Into:. Searching iterators are
provided too, such as findFirst:IfPresent:IfAbsent:.
New collections can be created from the elements of an existing collections using
asDictionary, asList, asSequence, asSet, etc. Two collections can be concatenated to
form a new collection using the comma message (,).
|