

% 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{Blocks - the Friends of Conditionals and Loops}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W2S08}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Remember: A Block Definition Freezes its Body}

\begin{itemize}
\item Evaluating a block definition does \textbf{not} execute its body 
\end{itemize}

\begin{listing}{}
[ 2 + 6 ] 
> [ 2 + 6 ] 
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Remember: Block Execution}

\begin{itemize}
\item Executing a Block is explicit 
\end{itemize}

\begin{listing}{}
[ 2 + 6 ] value
> 8 
\end{listing}

\begin{itemize}
\item and repeatable
\end{itemize}

\begin{listing}{}
| b |
b := [ 2 + 6 ].
b value.
> 8
b value
> 8 
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks are Used to Express Conditions}

\begin{listing}{}
max: anObject 
  "Answer the receiver or the argument, whichever has the greater anObject."

  ^ self > anObject
      ifTrue: [ self ]
      ifFalse: [ anObject ]
\end{listing}

Yes this is a message \code{ifTrue:ifFalse:} sent to a Boolean
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks are Used to Express Loops}

\begin{itemize}
\item Some simple loops
\item Printing 10 dots
\end{itemize}

\begin{listing}{}
10 timesRepeat: [ File stdout << '.' ]
> ............
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks are Used to Express Loops (2)}
% syntax highlighting = smalltalk

\begin{listing}{}
1 to: 10 do: [:i | File stdout << i ]
> 12345678910 
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks are Used to Express Loops (3)}

\begin{itemize}
\item A traditional \code{for} loop for i=1,100, i++
\end{itemize}

\begin{listing}{}
1 to: 100 by: 3 do: [:i | File stdout << i ]
> 147101316192225283134374043464952555861646
> 770737679828588919497100
\end{listing}

\begin{itemize}
\item The message \code{to:by:do:} is sent to an integer
\item \code{i} will get all the computed values one by one
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks are Used For Iterators}

\begin{itemize}
\item Basis for iterators
\end{itemize}

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

\begin{itemize}
\item Here the message is sent to the collection itself
\item See Lecture on Iterators
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Yes ifTrue:ifFalse: is a message!}

\begin{listing}{}
Weather isRaining
    ifTrue: [ self takeMyUmbrella ]
    ifFalse: [ self takeMySunglasses ]
\end{listing}

\begin{itemize}
\item Conceptually \code{ifTrue:ifFalse:} is a message sent to an object: a boolean!
\item \code{ifTrue:ifFalse:} is in fact radically optimized by the compiler
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation Note}

\begin{itemize}
\item Note that the Virtual Machine shortcuts calls to Boolean such as condition for speed reason
\item But you can implement your own conditional methods and debug to see that sending a message is dispatching to the right object
\item Implement your own control structure such as \code{siAlors:sinon:} (in French) and try it
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

\begin{itemize}
\item Blocks freeze and control computation 
\item Basis for 
\begin{itemize}
\item conditionals 
\item loops / iterators
\item exceptions (see future lectures)
\item concurrence 
\end{itemize}

\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:
