

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Iterators}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W3S09}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{What You Will Learn}

\begin{itemize}
\item Understand the power of iterators
\item Offer an overview of iterators
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Pharo code is Compact!}

\begin{listing}{}
ArrayList<String> strings = new ArrayList<String>();
for(Person person: persons)
    strings.add(person.name());
\end{listing}

is expressed as

\begin{listing}{}
strings := persons collect: [ :person | person name ]
\end{listing}

\begin{itemize}
\item Yes in Java 8.0 it is finally simpler 
\end{itemize}

\begin{listing}{}
strings = persons.stream().map(person -> person.getName())
\end{listing}

\begin{itemize}
\item But it is like that in Pharo since day one!
\item Iterators are deep into the core of the language and libraries
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A First Iterator - collect:}

 \code{collect:} applies the block to each element and returns a collection (of the same kind than the receiver) with the results

\begin{listing}{}
  #(2 -3 4 -35 4) collect: [ :each | each abs ]
  > #(2 3 4 35 4)
\end{listing}

\begin{itemize}
\item \code{collect:} evaluates the block for each element (using \code{value:})
\item In the block, each element is sent \code{abs} (absolute)  
\item \code{collect:} returns a new collection (of the same kind of the receiver) with all results
\item {[}Think object{]} We ask the collection to do something for us
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Another collect: Example}

We want to know if each elements is odd or even

\begin{listing}{}
#(16 11 68 19) collect: [ :i | i odd ]
\end{listing}

\begin{listing}{}
>  #(false true false true)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Choose your camp!}

\begin{listing}{}
  #(16 11 68 19) collect: [ :i | i odd ]
\end{listing}

We can also do it that way! (We copied the definition of \code{collect:})

\begin{listing}{}
| result |
aCol :=  #(16 11 68 19).
result := aCol species new: aCol size.
1 to: aCollection size do:
    [ :each | result at: each put: (aCol at: each) odd ].
^ result
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Part of the Collection Hierarchy}

Iterators work polymorphically on the entire collection hierarchy.
Below a part of the Collection hierarchy.


\begin{center}
\includegraphics[width=1.0\textwidth]{figures/CollectionHierarchy.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Think objects!}

\begin{itemize}
\item With iterators we \textbf{tell} the collection to \textbf{iterate on itself}
\item As a client we do not have to know the internal logic of the collection
\item Each collection can implement differently the iterator
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Basic Iterators Overview}

\begin{itemize}
\item \code{do:} (iterate)
\item \code{collect:} (iterate and collect results)
\item \code{select:} (select matching elements)
\item \code{reject:} (reject matching elements)
\item \code{detect:} (get first element matching)
\item \code{detect:ifNone:} (get first element matching or a default value)
\item \code{includes:} (test inclusion)
\item and a lot more...
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{do: an Action on Each Clement}

\begin{itemize}
\item Iterates on each elements
\item Applies the block on each elements
\end{itemize}

\begin{listing}{}
#(16 11 68 19) do: [ :each | Transcript show: each ; cr ]
\end{listing}

Here we print each element and insert a carriage return


\begin{center}
\includegraphics[width=0.4\textwidth]{figures/Iterators-Transcript2.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{select: Elements Matching a Criteria}

To select some elements, use \code{select:}

\begin{listing}{}
  #(16 11 68 19) select: [ :i | i odd ]
  > #(11 19)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{With Unary Messages, No Block Needed}

When a block is just one message, we can pass an unary message selector

\begin{listing}{}
#(16 11 68 19) select: [ :i | i odd ]
\end{listing}

is equivalent to

\begin{listing}{}
#(16 11 68 19) select: #odd
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{reject: Some Elements Matching a Criteria}

To filter some elements, use \code{reject:}

\begin{listing}{}
#(16 11 68 19) reject:  [ :i | i odd ]
> #(16 68)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{detect: The First Elements That...}

To find the first element that matches, use \code{detect:}

\begin{listing}{}
#(16 11 68 19) detect:  [ :i | i odd ]
> 11
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{detect:ifNone:}

To find the first element that matches else return a value, use \code{detect:ifNone:}

\begin{listing}{}
#(16 12 68 20) detect:  [ :i | i odd ] ifNone: [ 0 ]
> 0
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Some Powerful Iterators}

\begin{itemize}
\item \code{anySatisfy:} (tests if one object is satisfying the criteria)
\item \code{allSatisfy:} (tests if all objects are satisfying the criteria)
\item \code{reverseDo:} (do an action on the collection starting from the end)
\item \code{doWithIndex:} (do an action with the element and its index)
\item \code{pairsDo:} (evaluate aBlock with my elements taken two at a time.)
\item \code{permutationsDo:} ...
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Iterating Two Structures}

To iterate \code{with:do:}

\begin{listing}{}
#(1 2 3)
    with:  #(10 20 30)
    do: [ :x :y | Transcript show: (y * x) ; cr ]
\end{listing}


\begin{center}
\includegraphics[width=0.7\textwidth]{figures/Iterator-WithDo-Transcript.png}\end{center}


\code{with:do:} requires two structures of the same length
\end{frame}



\begin{frame}[fragile]

\frametitle{Use do:separatedBy:}

\begin{listing}{}
String streamContents: [ :s |
    #('a' 'b' 'c')
        do: [ :each | s << each ]
        separatedBy: [ s << ', ' ]
]
> 'a, b, c'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Grouping Elements}

To group elements according to a grouping function: \code{groupedBy:}

\begin{listing}{}
#(1 2 3 4 5 6 7) groupedBy: #even
> a PluggableDictionary(false->#(1 3 5 7) true->#(2 4 6) )
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Flattening Results}

How to remove one level of nesting in a collection? Use \code{flatCollect:}

\begin{listing}{}
#( #(1 2) #(3) #(4) #(5 6)) collect: [ :each | each ]
> #(#(1 2) #(3) #(4) #(5 6)))
\end{listing}

\begin{listing}{}
#( #(1 2) #(3) #(4) #(5 6)) flatCollect: [ :each | each ]
> #(1 2 3 4 5 6 )
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Opening The Box}

\begin{itemize}
\item You can learn and discover the system
\item You can define your own iterator
\item For example how \code{do:} is implemented?
\end{itemize}

\begin{listing}{}
SequenceableCollection >> do: aBlock
  "Evaluate aBlock with each of the receiver's elements as the argument."

  1 to: self size do: [:i | aBlock value: (self at: i)]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Analysis}

\begin{itemize}
\item Iterators are really powerful because they support polymorphic code
\item All the collections support them
\item New ones are defined
\item Missing controlled navigation as in the Iterator design pattern
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

\begin{itemize}
\item Iterators are your best friends
\item Simple and powerful
\item Enforce encapsulation of collections and containers
\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
