Iterators
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
- Understand the power of iterators
- Offer an overview of iterators
Pharo code is Compact!
is expressed as
- Yes in Java 8.0 it is finally simpler
- But it is like that in Pharo since day one!
- Iterators are deep into the core of the language and libraries
A First Iterator - collect:
collect:
applies the block to each element and returns a collection (of the same kind than the receiver) with the results
collect:
evaluates the block for each element (using value:
)
- In the block, each element is sent
abs
(absolute)
collect:
returns a new collection (of the same kind of the receiver) with all results
- [Think object] We ask the collection to do something for us
Another collect: Example
We want to know if each elements is odd or even
Choose your camp!
We can also do it that way! (We copied the definition of collect:
)
Part of the Collection Hierarchy
Iterators work polymorphically on the entire collection hierarchy.
Below a part of the Collection hierarchy.
Think objects!
- With iterators we tell the collection to iterate on itself
- As a client we do not have to know the internal logic of the collection
- Each collection can implement differently the iterator
Basic Iterators Overview
do:
(iterate)
collect:
(iterate and collect results)
select:
(select matching elements)
reject:
(reject matching elements)
detect:
(get first element matching)
detect:ifNone:
(get first element matching or a default value)
includes:
(test inclusion)
- and a lot more...
do: an Action on Each Clement
- Iterates on each elements
- Applies the block on each elements
Here we print each element and insert a carriage return
select: Elements Matching a Criteria
To select some elements, use select:
With Unary Messages, No Block Needed
When a block is just one message, we can pass an unary message selector
is equivalent to
reject: Some Elements Matching a Criteria
To filter some elements, use reject:
detect: The First Elements That...
To find the first element that matches, use detect:
detect:ifNone:
To find the first element that matches else return a value, use detect:ifNone:
Some Powerful Iterators
anySatisfy:
(tests if one object is satisfying the criteria)
allSatisfy:
(tests if all objects are satisfying the criteria)
reverseDo:
(do an action on the collection starting from the end)
doWithIndex:
(do an action with the element and its index)
pairsDo:
(evaluate aBlock with my elements taken two at a time.)
permutationsDo:
...
Iterating Two Structures
To iterate with:do:
with:do:
requires two structures of the same length
Use do:separatedBy:
Grouping Elements
To group elements according to a grouping function: groupedBy:
Flattening Results
How to remove one level of nesting in a collection? Use flatCollect:
Opening The Box
- You can learn and discover the system
- You can define your own iterator
- For example how
do:
is implemented?
Analysis
- Iterators are really powerful because they support polymorphic code
- All the collections support them
- New ones are defined
- Missing controlled navigation as in the Iterator design pattern
Summary
- Iterators are your best friends
- Simple and powerful
- Enforce encapsulation of collections and containers
/