

% 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{An Overview of Essential Collections}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W3S07}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{What You Will Learn}

\begin{itemize}
\item Some basic collections
\item Essential API to program collections
\item Difference between literal and dynamic arrays
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Collection Common Attributes}

\begin{itemize}
\item Pharo has a rich hierarchy of collection
\item Common API: \code{size}, \code{do:}, \code{select:}, \code{includes:}, \code{collect:}...
\item First element is at index \code{1}
\item Can contain any object
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Most Common Collections}

\begin{itemize}
\item \code{OrderedCollection} (dynamically growing)
\item \code{Array} (fixed size, direct access)
\item \code{Set} (no duplicates)
\item \code{Dictionary} (key-based, \textit{aka.} maps)
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Essential Collection In a Nutshell}


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

\end{frame}



\begin{frame}[fragile]

\frametitle{Common API Overview}

Common messages work on all collections

\begin{enumerate}
\item creation: \code{with: anElt}, \code{with:with:}, \code{withAll: aCollection}
\item accessing: \code{size}, \code{at: anIndex}, \code{at: anIndex put: anElt}
\item testing: \code{isEmpty}, \code{includes: anElt}, \code{contains: aBlock}, 
\item adding: \code{add: anElement}, \code{addAll: aCollection}
\item removing: \code{remove: anElt}, \code{remove: anElt ifAbsent: aBlock}, \code{removeAll: aCollection}
\item enumerating: \code{do: aBlock}, \code{collect: aBlock}, \code{select: aBlock}, \code{reject: aBlock}, \code{detect: aBlock}, ...
\item converting: \code{asBag}, \code{asSet}, \code{asOrderedCollection}, \code{asSortedCollection}, \code{asArray}
\end{enumerate}
\end{frame}



\begin{frame}[fragile]

\frametitle{Variable Size Object Creation}

\begin{itemize}
\item Message \code{new} instantiates one object
\item Message \code{new: size} creates an object specifying its size
\end{itemize}

\begin{listing}{}
Array new: 4
> #(nil nil nil nil)
\end{listing}

\begin{listing}{}
Array new: 2
> #(nil nil)
\end{listing}

\begin{listing}{}
(OrderedCollection new: 1000)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{With Specific Elements}

\begin{listing}{}
OrderedCollection withAll: #(7 7 3 13)
> an OrderedCollection(7 7 3 13)
\end{listing}

\begin{listing}{}
Set withAll: #(7 7 3 13)
> a Set( 7 3 13)
\end{listing}

Remember: no duplicate in Sets
\end{frame}



\begin{frame}[fragile]

\frametitle{Creation  with Default Value}

\begin{listing}{}
OrderedCollection new: 5 withAll: 'a' 
> an OrderedCollection('a' 'a' 'a' 'a' 'a')
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{First Element Starts At 1}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') at: 2
> 'hates'

#('Calvin' 'hates' 'Suzie') asOrderedCollection  at: 2 
> 'hates'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\label{arrays}
\frametitle{Collections can be Heterogenous }

Collections can contain any sort of objects

\begin{listing}{}
#('calvin' (1 2 3))
> #('calvin' #(1 2 3))
\end{listing}

\begin{itemize}
\item An array composed of a string and an array
\end{itemize}

\begin{listing}{}
| s |
s := Set new. 
s add: Set new; 
    add: 1;
    add: 2.
s asArray 
> an Array(1 2 a Set())
\end{listing}

\begin{itemize}
\item A set containing an empty set and some numbers
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Iteration}

\begin{itemize}
\item Using message \code{do: aBlock}
\item But many iterators (see Iterators Lecture)
\end{itemize}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') 
     do: [ :each | Transcript show: each ; cr ]
\end{listing}


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

\end{frame}



\begin{frame}[fragile]

\frametitle{Arrays}

\begin{itemize}
\item Fixed size collection
\item Direct access: \code{at:} and \code{at:put:}
\item Has literal syntax: \code{\#( ... )} 
\item Can also be created using \code{new:} 
\end{itemize}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') size
> 3
\end{listing}

is equivalent to 

\begin{listing}{}
((Array new: 3) 
    at: 1 put: 'Calvin'; 
    at: 2 put: 'hates'; 
    at: 3 put: 'Suzie') size
> 3
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Accessing Elements}

Getting the size of a collection

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') size
> 3
\end{listing}

Accessing the 2nd element using \code{at: anIndex}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') at: 2
> 'hates'
\end{listing}

Remember collection index starts at 1
\end{frame}



\begin{frame}[fragile]

\frametitle{Accessing Out of Bounds Elements}

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') at: 55
> SubscriptOutOfBounds Error
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Modifying Elements}

Use the message \code{at: anIndex put: anObject} 

Modifying the second element of the receiver

\begin{listing}{}
#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'
> #('Calvin' 'loves' 'Suzie')
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Literal Arrays}

Literal arrays contain objects that have a textual (literal) representation: numbers, strings, nil, symbols

\begin{listing}{}
#(45 'milou' 1300 true #tintin)
> #(45 'milou' 1300 true #tintin)
\end{listing}

They are instances of the class \code{Array}

\begin{listing}{}
#(45 38 1300 8) class
> Array
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Literals Arrays are Array Instances}

Literal arrays are equivalent to a dynamic version

A literal array

\begin{listing}{}
#(45 38 'milou' 8) 
> #(45 38 'milou' 8) 
\end{listing}

An array

\begin{listing}{}
Array with: 45 with: 38 with: 'milou' with: 8
> #(45 38 'milou' 8) 
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{OrderedCollection}

\begin{itemize}
\item Sequenceable
\item Growing size
\item \code{add:}, \code{remove:}
\end{itemize}

\begin{listing}{}
| ordCol |
ordCol := OrderedCollection new.
ordCol add: 'Reef'; add: 'Pharo'; addFirst: 'Pharo'.
ordCol 
> an OrderedCollection('Pharo' 'Reef' 'Pharo')
ordCol add: 'Seaside'.
ordCol
> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Seaside')
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Conversion}

\begin{listing}{}
#('Pharo' 'Reef' 'Pharo' 'Pharo') asOrderedCollection
> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Pharo')
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Set}

\begin{itemize}
\item No duplicates
\item Growing size
\item \code{add:}, \code{remove:}
\item Can contain any object including other Sets
\end{itemize}

\begin{listing}{}
#('Pharo' 'Reef' 'Pharo' 'Pharo') asSet
> a Set('Pharo' 'Reef')
\end{listing}

\begin{listing}{}
Set with: (Set with: 1) with: (Set with: 2)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Conversion}

Collections can be converted simply to other collections

\begin{listing}{}
  asOrderedCollection

  asSet

  asArray
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Dictionary}

\begin{itemize}
\item Key/values
\item Growing size
\item Accessing \code{at:}, \code{at:ifAbsent:}
\item Changing/adding \code{at:put:}, \code{at:ifAbsentPut:} 
\item Iterating: \code{do:}, \code{keysDo:}, \code{keysAndValuesDo:}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Dictionary Creation}

\begin{listing}{}
| days |
days := Dictionary new. 
days   
  at: #January put: 31;
  at: #February put: 28;
  at: #March put: 31.  
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Alternate Dictionary Creation}

\begin{listing}{}
| days |
days := Dictionary new. 
days   
  at: #January put: 31;
  at: #February put: 28;
  at: #March put: 31.  
\end{listing}

is equivalent to 

\begin{listing}{}
{ #January -> 31.
  #February -> 28.
  #March -> 31} asDictionary
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Pairs}

\begin{listing}{}
(#January -> 31) key 
> #January
\end{listing}

\begin{listing}{}
(#January -> 31) value
> 31
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Dictionary Access}

\begin{listing}{}
  | days |
  days := Dictionary new. 
  days   
    at: #January put: 31;
    at: #February put: 28;
    at: #March put: 31.  
\end{listing}

\begin{listing}{}
  days at: #January 
  > 31 

  days at: #NoMonth
  > KeyNotFound Error

  days at: #NoMonth ifAbsent: [0]
  > 0 
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Dictionary Iteration}

\begin{listing}{}
days do: [ :each | Transcript show: each ;cr ] 
\end{listing}

prints

\begin{listing}{}
  31
  28
  31
\end{listing}

Why? 
Because

\begin{listing}{}
Dictionary >> do: aBlock

  ^self valuesDo: aBlock
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Keys and Values Iteration}

\begin{listing}{}
days keysAndValuesDo: 
  [ :k :v | Transcript show: k asString, ' has ',  v printString, ' days' ; cr ] 
\end{listing}

shows: 

\begin{listing}{}
  January has 31 days
  February has 28 days
  March has 31 days
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

\begin{itemize}
\item Easy to use collections
\item Common vocabulary
\item Simple conversion between them
\item Easy to discover!
\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:
