% !TEX program = xelatex
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
%\usepackage{chessboard}
%\usepackage[LSBC4,T1]{fontenc}
\usepackage{../styles/slides}
\usepackage{../styles/mygraphs}
\usepackage{../styles/langs}
\usetikzlibrary{shapes}
% \usepackage{../data}
\hfuzz=220pt
%\setmonofont[Scale=.88]{Consolas}
%\newfontfamily{\consolas}{Consolas}
\lstset{language=Scala,
style=mystyle,
numbersep=0pt,
numbers=none,
xleftmargin=0mm}
\newcommand{\bl}[1]{\textcolor{blue}{#1}}
% beamer stuff
\renewcommand{\slidecaption}{PEP (Scala) 02, King's College London}
\newcommand{\UParrow}[3]{%
\begin{textblock}{0}(#2,#3)%
\onslide<#1>{%
\begin{tikzpicture}%
\node at (0,0) [single arrow, shape border rotate=90, fill=red,text=red]{a};%
\end{tikzpicture}}%
\end{textblock}}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
\frametitle{%
\begin{tabular}{@ {}c@ {}}
\\[5mm]
\huge PEP Scala (2)
\end{tabular}}
\normalsize
\begin{center}
\begin{tabular}{ll}
Email: & christian.urban at kcl.ac.uk\\
%Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
Slides \& Code: & KEATS\bigskip\\
Office Hour: & Fridays 11:00 -- 12:00\\
Location: & N7.07 (North Wing, Bush House)\bigskip\\
Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ \\
\end{tabular}
\end{center}
\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[t,fragile]
% \frametitle{For-Comprehensions}
% %\small
% \begin{lstlisting}[language=Scala,numbers=none]
% for (n <- List(1, 2, 3, 4, 5)) yield n * n
% \end{lstlisting}
% \begin{textblock}{5}(2,6)
% \includegraphics[scale=0.3]{../pics/fun.png}
% \end{textblock}
% \begin{textblock}{5}(9,6)
% \includegraphics[scale=0.3]{../pics/fun.png}
% \end{textblock}
% \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
\frametitle{For-Comprehensions}
\begin{center}
\begin{tikzpicture}[scale=1,
node/.style={
rectangle,rounded corners=3mm,
very thick,draw=black!50,
minimum height=18mm, minimum width=20mm,
top color=white,bottom color=black!20}]
\node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
\node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
\node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
\node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
\node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
\node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
\node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
\onslide<2->{
\node (B0) at (1.4,-3) {\texttt{List(}};
\node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
\node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
\node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
\node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
\node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
\onslide<2->{
\draw [->,line width=1mm] (A1.south) -- (B1.north);
\draw [->,line width=1mm] (A2.south) -- (B2.north);
\draw [->,line width=1mm] (A3.south) -- (B3.north);
\draw [->,line width=1mm] (A4.south) -- (B4.north);
\draw [->,line width=1mm] (A5.south) -- (B5.north);}
\onslide<2->{
\node (Q1) at (-0.45,-0.1) {};
\node (Q2) at (-0.45,-2.8) {};
\node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
\draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
\end{tikzpicture}
\end{center}
\onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[t]
% \frametitle{For-Comprehensions Again}
% \begin{center}
% \begin{tikzpicture}[scale=1,
% node/.style={
% rectangle,rounded corners=3mm,
% very thick,draw=black!50,
% minimum height=18mm, minimum width=20mm,
% top color=white,bottom color=black!20}]
% \node (A0) at (0,0)
% {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5))
% \textcolor{purple}{\textbf{yield}} n\,*\,n}};
% \node (A1) at (0,-1.5) {\LARGE\textbf{vs}};
% \node (A2) at (0,-3)
% {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5)) println(n)}};
% \end{tikzpicture}
% \end{center}\bigskip
% The second version is in case the for \textbf{does not}
% produce any result.
% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
%\frametitle{Option Type}
Find something below 4 in a list. What do you think Scala answers?\bigskip\bigskip
\begin{onlyenv}<1>
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
List(7,2,3,4,5,6).find(_ < 4)
List(5,6,7,8,9).find(_ < 4)
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
List(7,2,3,4,5,6).find(_ < 4)
res: Option[Int] = Some(2)
List(5,6,7,8,9).find(_ < 4)
res: Option[Int] = None
\end{lstlisting}
\end{onlyenv}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
\frametitle{Option Type}
\begin{itemize}
\item if the value is present, you use\bigskip
\begin{center}\pcode{Some(value)}\end{center}\bigskip\bigskip
\item if no value is present, you use\bigskip
\begin{center}\pcode{None}\end{center}\bigskip\bigskip
\end{itemize}
\small e.g.~\code{Option[Int]}, then \code{Some(42)} and \code{None}\\
good for error handling
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Option Type}
\small
\begin{onlyenv}<1>
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
Integer.parseInt("1234")
// vs.
def get_me_an_int(s: String) : Option[Int] =
Try(Some(Integer.parseInt(s))).getOrElse(None)
\end{lstlisting}
\end{onlyenv}\bigskip\bigskip\bigskip
in the Scala code it is clear from the type I that have to deal
with the \pcode{None}-case; no JavaDoc needed
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Higher-Order Functions}
In Scala, functions can take other functions as arguments and can return
a function as a result.\bigskip\bigskip
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
List(7,2,3,4,5,6).find(_ < 4)
\end{lstlisting}
\UParrow{1}{8}{11}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Higher-Order Functions (2)}
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
def even(x: Int) : Boolean = x % 2 == 0
List(1, 2, 3, 4, 5).filter(even)
res : List[Int] = List(2, 4)
List(1, 2, 3, 4, 5).count(even)
res : Int = 2
List(1, 2, 3, 4, 5).find(even)
res: Option[Int] = Some(2)
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Anonymous Functions}
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
def less4(x: Int) = x < 4
\end{lstlisting}
\begin{center}
vs
\end{center}
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
(x: Int) => x < 4
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{map (lower case)}
applies a function to each element of a list (and more)
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node (A0) at (1.2,0) {\texttt{List(\,}};
\node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
\node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
\node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
\node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
\node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
\node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
\node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
\node (A8) at (8.3,0) {\texttt{8)}};
\node (B0) at (1.2,-3) {\texttt{List(\,}};
\node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
\node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
\node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
\node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
\node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
\node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
\node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
\node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
\draw [->,line width=1mm] (A1.south) -- (B1.north);
\draw [->,line width=1mm] (A2.south) -- (B2.north);
\draw [->,line width=1mm] (A3.south) -- (B3.north);
\draw [->,line width=1mm] (A4.south) -- (B4.north);
\draw [->,line width=1mm] (A5.south) -- (B5.north);
\draw [->,line width=1mm] (A6.south) -- (B6.north);
\draw [->,line width=1mm] (A7.south) -- (B7.north);
\draw [->,line width=1mm] (A8.south) -- (B8.north);
\node [red] (Q0) at (-0.5,-0.3) {\large\texttt{n}};
\node (Q1) at (-0.5,-0.4) {};
\node (Q2) at (-0.5,-2.5) {};
\node [red] (Q3) at (-0.5,-2.65) {\large\texttt{n\,*\,n}};
\draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);
\node [red] at (-1.5,-1.5) {\Large{}\it\textbf{map}};
\end{tikzpicture}
\end{center}\bigskip
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
List(1,2,3,4,5,6,7,8).map(n => n * n)
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{For-Comprehensions are maps}
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
for (n <- List(1,2,3,4,5,6,7,8))
yield n * n
// is just syntactic sugar for
List(1,2,3,4,5,6,7,8).map(n => n * n)
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Map (upper case)}
a type, representing a key-value association datastructure\bigskip\bigskip
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
val ascii =
('a' to 'z').map(c => (c, c.toInt))
val ascii_Map = ascii.toMap
ascii_Map.get('a') // -> 97
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Pattern Matching}
\ldots on pairs:\bigskip
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
def fizz_buzz(n: Int) : String =
(n % 3, n % 5) match {
case (0, 0) => "fizz buzz"
case (0, _) => "fizz"
case (_, 0) => "buzz"
case _ => n.toString
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Recursion}
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
def fib(n: Int) : Int = {
if (n == 0 || n == 1) 1
else fib(n - 1) + fib(n - 2)
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Recursion}
\small
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-4mm]
def my_flatten(xs: List[Option[Int]]): List[Int] =
xs match {
case Nil => Nil
case None :: rest => my_flatten(rest)
case Some(v) :: rest => v :: my_flatten(rest)
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
\begin{center}
\begin{tabular}[t]{@{}l@{}l@{}}
\includegraphics[scale=0.1]{../pics/mand4.png} & \hspace{4mm}
\raisebox{0mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}
\end{tabular}
\end{center}
\begin{center}
My Office Hours: Thursdays 12 -- 14\\
And specifically for Scala: Tuesdays 10:45 -- 11:45
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[t]
% \begin{center}
% \includegraphics[scale=0.3]{../pics/blow.png}
% \end{center}
% \begin{textblock}{14}(2,11.4)
% \large\bf{}Mind-Blowing Programming Languages:\\
% Overloading in any language is great but it makes a difference\; \code{10/3}
% \;or\; \code{10.0/3}
% \end{textblock}
% \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[t]
% \begin{center}
% \includegraphics[scale=0.3]{../pics/blow.png}
% \end{center}
% \begin{textblock}{14}(2,11.4)
% \large\bf{}Mind-Blowing Programming Languages:\\
% \centering PHP \textcolor{gray}{(7.0)}
% \end{textblock}
% \end{frame}
% %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
\frametitle{Jumping Towers}
\begin{center}
\begin{tikzpicture}[scale=1.2]
\draw[line width=1mm,cap=round] (0,0) -- (5,0);
\draw[line width=1mm,cap=round] (0,1) -- (5,1);
\draw[line width=1mm,cap=round] (0,0) -- (0,1);
\node at (0.5,0.5) {\textbf{\Large 3}};
\draw[line width=1mm,cap=round] (1,0) -- (1,1);
\node at (1.5,0.5) {\textbf{\Large 4}};
\draw[line width=1mm,cap=round] (2,0) -- (2,1);
\node at (2.5,0.5) {\textbf{\Large 2}};
\draw[line width=1mm,cap=round] (3,0) -- (3,1);
\node at (3.5,0.5) {\textbf{\Large 0}};
\draw[line width=1mm,cap=round] (4,0) -- (4,1);
\node at (4.5,0.5) {\textbf{\Large 1}};
\draw[line width=1mm,cap=round] (5,0) -- (5,1);
\draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (1.5,1);
\draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (2.5,1);
\draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (3.5,1);
\draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (3.5,0);
\draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (4.5,0);
\draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (4.5,1) to (5.7,1);
\node at (5.7, 0.8) {End};
\end{tikzpicture}
\end{center}\bigskip
shortest: 3 $\rightarrow$ 4 $\rightarrow$ End
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
\frametitle{``Children'' / moves}
\begin{center}
\begin{tikzpicture}
[grow=right,level distance=30mm,child anchor=north,line width=0.5mm]
\node {$[3,4,2,0,1]$}
child {node {$[0,1]$}}
child {node {$[2,0,1]$}
child {node {$[1]$} child [level distance=13mm] {node {End}}}
child {node {$[0,1]$}}
}
child {node {$[4,2,0,1]$\ldots}};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}<1-10>[t]
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t,fragile]
\frametitle{\mbox{}\hspace{40mm}\textbf{???}}
\begin{textblock}{5}(2,6)
\includegraphics[scale=0.35]{../pics/commits.png}
\end{textblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: