

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

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Blocks}

Blocks are:

\begin{itemize}
\item kind of anonymous methods
\begin{itemize}
\item also called (lexical) closures
\end{itemize}

\item used everywhere in Pharo
\begin{itemize}
\item loops, conditionals, iterators, ...
\item GUI frameworks, DSLs, ...
\item at the heart of the system
\end{itemize}

\item just introduced in Java 8.0
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Block Definition}

A block is \textbf{defined} by \code{{[} {]}}

\begin{listing}{}
[ expressions. ... ]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Block Definition Does not Execute Code}

\begin{itemize}
\item Executing code may signal an Error
\end{itemize}

\begin{listing}{}
( 1 / 0 )
-> Error
\end{listing}

\begin{itemize}
\item But, no error when \textbf{defining} a block
\begin{itemize}
\item a block definition does not execute its body
\item a block definition freezes its body computation
\end{itemize}

\end{itemize}

\begin{listing}{}
[ 1 / 0 ]
> [ 1 /0 ]
\end{listing}

\begin{listing}{}
[ 1 / 0 ].
1 + 2 
> 3
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Executing a Block}

Executing a block is done \textbf{explicitly} through \code{value}

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

\begin{listing}{}
[ 1 / 0 ] value
> Error
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Block with 1 Argument}

A block can take arguments (just like methods)

\begin{listing}{}
[ :x | x + 2 ]
\end{listing}

\begin{itemize}
\item \code{{[} {]}} delimits the block
\item \code{:x} is a block argument
\item \code{x + 2} is the block body
\end{itemize}

\begin{listing}{}
[ :x | x + 2 ] value: 5
> 7
\end{listing}

\begin{itemize}
\item \code{value: 5} executes the block with 5 as argument
\begin{itemize}
\item \code{x} is 5 during the block execution
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Block Execution Value}

Block execution returns the value of the last expression

\begin{listing}{}
[ :x |
  x + 33.
  x + 2 ] value: 5
> 7
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Blocks can be Stored}

\begin{itemize}
\item A block can be stored in a variable
\item A block can be evaluated multiple times
\end{itemize}

\begin{listing}{}
| add2 |
add2 := [ :x | x + 2 ].

add2 value: 5.
> 7

add2 value: 33
> 35
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Defining a Block with 2 Arguments}

Example:

\begin{listing}{}
[ :x :y | x + y ]
\end{listing}

\code{ :x :y } are block arguments

How to execute a block with two arguments?

\begin{listing}{}
[ :x :y | x + y ] ??? 5 7
> 12
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Executing a Block with 2 Arguments}

\begin{listing}{}
[ :x :y | x + y ] value: 5 value: 7
> 12
\end{listing}

\begin{itemize}
\item \code{value: 5 value: 7} evaluates the block with 5 and 7
\begin{itemize}
\item \code{x} is 5 and \code{y} is 7 during the block evaluation
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Block with Temporary Variables}

Blocks can define temp. variables (just like methods)

\begin{listing}{}
Collection>>affect: anObject when: aBoolean
  self do: [ :index | | args |
    args := ....
    aBoolean
      ifTrue: [ anObject do: args ]
      ifFalse: [ anObject doDifferently: args ] ].
\end{listing}

\begin{itemize}
\item \code{\textbar{} args \textbar{} } defines a temporary variable named \code{args}
\item \code{args} exists only during block evaluation
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Returning from a Block Returns from the Method}

When a return \code{\string^} is executed in a block, computation exits the method defining the block

\begin{listing}{}
Integer>>factorial
  "Answer the factorial of the receiver."
  
  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'
\end{listing}

\begin{listing}{}
0 factorial
>1

42 factorial
>1405006117752879898543142606244511569936384000000000
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Design Advice}

\begin{itemize}
\item Use blocks with 2 or 3 arguments maximum
\item Define a class instead of a block for more arguments
\item A block encapsulates only 1 computation
\begin{itemize}
\item it cannot define more facets (e.g., printing)
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary on Blocks}

\begin{listing}{}
[ :variable1 :variable2 ... |
  | tmp |
  expression1.
  ... variable1 ...
] value: ... value: ...
\end{listing}

\begin{itemize}
\item Kind of anonymous method
\item Technically lexical closures
\item Can be stored in variables and method arguments
\item Basis of conditionals, loops and iterators (see companion lectures)
\item Further readings: \url{http://deepintopharo.org}
\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:
