% !TEX program = xelatex+ −
\documentclass[dvipsnames,14pt,t,xelatex]{beamer}+ −
%\usepackage{chessboard}+ −
%\usepackage[LSBC4,T1]{fontenc}+ −
\usepackage{../slides}+ −
\usepackage{../graphics}+ −
\usepackage{../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 Hours: & Thursdays 12:00 -- 14:00\\+ −
Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ + −
\end{tabular}+ −
\end{center}+ −
+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
\begin{frame}[c,fragile]+ −
\frametitle{Scala 2.13.1}+ −
+ −
\begin{lstlisting}[language={},numbers=none,+ −
basicstyle=\ttfamily\small,xleftmargin=-2mm]+ −
$ scala+ −
+ −
Welcome to Scala 2.13.1 (Java HotSpot(TM) + −
64-Bit Server VM, Java 9). Type in expressions + −
for evaluation. Or try :help.+ −
+ −
scala>+ −
\end{lstlisting}%$+ −
\bigskip\bigskip+ −
+ −
With older versions you will get strange results with my reference implementation.+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
\begin{frame}[c,fragile]+ −
\frametitle{Reference Implementation}+ −
+ −
Keep your implementation and my reference implementation separate.\bigskip+ −
+ −
\begin{lstlisting}[language={},numbers=none,+ −
basicstyle=\ttfamily\small,xleftmargin=-2mm]+ −
$ scala -cp collatz.jar+ −
+ −
scala> CW6a.collatz(6)+ −
res0: Long = 8+ −
+ −
+ −
scala> import CW6a._+ −
scala> collatz(9)+ −
res1: Long = 19+ −
\end{lstlisting}%$+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
\begin{frame}[t]+ −
\frametitle{Preliminary Part 7}+ −
+ −
\Large+ −
\[+ −
\texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)} + −
\]\bigskip+ −
+ −
\large+ −
\quad{}\;where \;$d_1^2$\; means \;$d_1 \cdot d_1$\; and so on+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
\begin{frame}[c]+ −
\frametitle{Discussion Forum}+ −
+ −
\large+ −
``Since we can't use \code{var}s I was wondering if we could use a stack?''+ −
\bigskip\bigskip\bigskip\bigskip+ −
+ −
\small+ −
My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each. + −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
\begin{frame}[t]+ −
\frametitle{Email: Hate 'val'}+ −
+ −
\mbox{}\\[-22mm]\mbox{}+ −
+ −
\begin{center}+ −
\begin{bubble}[10.5cm]+ −
Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\+ −
+ −
Hello Mr Urban,\medskip\\+ −
+ −
I just wanted to ask, how are we suppose to work+ −
with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is+ −
this rule active at all? I’ve spent 4 hours not thinking on the+ −
coursework, but how to bypass this annoying rule. What’s the whole+ −
point of all these coursework, when we can’t use everything Scala+ −
gives us?!?\medskip\\+ −
+ −
Regards.\\+ −
\mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\+ −
\end{bubble}+ −
\end{center}+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
\begin{frame}[c]+ −
+ −
\mbox{}\\[-25mm]\mbox{}+ −
+ −
\begin{center}+ −
\begin{bubble}[10.5cm]+ −
Subject: \textbf{Re: Hate '\textbf{\texttt{val}}'}\hfill 01:02 AM\bigskip\bigskip\\+ −
+ −
\textcolor{black!70}{+ −
\textit{\large<<my usual rant about fp\ldots\\ concurrency bla bla\ldots{} better programs+ −
yada>>}}\bigskip\bigskip\bigskip+ −
+ −
PS: What are you trying to do where you desperately want to use \texttt{var}?+ −
\end{bubble}+ −
\end{center}+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
\begin{frame}[c,fragile]+ −
+ −
\begin{textblock}{6}(0.5,0.5)+ −
\begin{bubble}[11.5cm]+ −
\small + −
Subject: \textbf{Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:04 AM\medskip\\+ −
+ −
\textbf{Right now my is\_legal function works fine:}+ −
+ −
\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]+ −
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = {+ −
var boolReturn = false+ −
if(x._1 > dim || x._2 > dim || x._1 < 0 || x._2 < 0) {+ −
else { var breakLoop = false+ −
if(path == Nil) { boolReturn = true }+ −
else { for(i <- 0 until path.length) {+ −
if(breakLoop == false) {+ −
if(path(i) == x) {+ −
boolReturn = true+ −
breakLoop = true+ −
}+ −
else { boolReturn = false }+ −
} else breakLoop+ −
}+ −
}+ −
boolReturn+ −
}+ −
\end{lstlisting}+ −
\end{bubble}+ −
\end{textblock}+ −
+ −
\begin{textblock}{6}(8.2,11.8)+ −
\begin{bubble}[5.5cm]\footnotesize\bf+ −
\ldots{}but I can’t make it work with boolReturn being val. What approach would+ −
you recommend in this case, and is using var in this case justified?+ −
\end{bubble}+ −
\end{textblock}+ −
+ −
\only<2>{+ −
\begin{textblock}{6}(0.3,11.8)+ −
\begin{bubble}[3.1cm]+ −
\textbf{Me:}+ −
\raisebox{-12mm}{\includegraphics[scale=0.08]{../pics/throwup.jpg}}+ −
\end{bubble}+ −
\end{textblock}}+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
\begin{frame}[t,fragile]+ −
+ −
\mbox{}\\[-25mm]\mbox{}+ −
+ −
\begin{textblock}{6}(0.5,2)+ −
\begin{bubble}[11.5cm]+ −
Subject: \textbf{Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:06 AM\bigskip\\+ −
\small+ −
+ −
OK. So you want to make sure that the \texttt{x}-position is not outside the+ −
board....and furthermore you want to make sure that the \texttt{x}-position+ −
is not yet in the path list. How about something like\bigskip+ −
+ −
\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]+ −
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = + −
...<<some board conditions>>... && !path.contains(x)+ −
\end{lstlisting}\bigskip+ −
+ −
\small Does not even contain a \texttt{val}.+ −
\end{bubble}+ −
\end{textblock}+ −
+ −
\begin{textblock}{6}(7,12)+ −
\footnotesize\textcolor{black!50}{(This is all on one line)}+ −
\end{textblock}+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
\begin{frame}[t,fragile]+ −
+ −
\mbox{}\\[-15mm]\mbox{}+ −
+ −
\begin{textblock}{6}(1,3)+ −
\begin{bubble}[10.5cm]+ −
Subject: \textbf{Re: Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 11:02 AM\bigskip\bigskip\\+ −
+ −
THANK YOU! You made me change my coding perspective. Because of you,+ −
I figured out the next one\ldots+ −
\end{bubble}+ −
\end{textblock}+ −
+ −
\only<2>{+ −
\begin{textblock}{6}(0.3,11.8)+ −
\begin{bubble}[3.1cm]+ −
\textbf{Me:}+ −
\raisebox{-12mm}{\includegraphics[scale=0.15]{../pics/happy.jpg}}+ −
\end{bubble}+ −
\end{textblock}}+ −
+ −
\end{frame}+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
+ −
+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + −
\begin{frame}[c]+ −
\frametitle{Assignments}+ −
+ −
Don't change any names or types in the templates!\bigskip+ −
+ −
Avoid at all costs:+ −
+ −
\begin{itemize}+ −
\item \code{var} + −
\item \code{return} + −
\item \texttt{ListBuffer}+ −
\item \texttt{mutable}+ −
\item \texttt{.par} + −
\end{itemize}\bigskip\bigskip+ −
+ −
I cannot think of a good reason to use stacks.+ −
\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.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}{10}{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{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}+ −
+ −
+ −
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ −
+ −
\end{document}+ −
+ −
%%% Local Variables: + −
%%% mode: latex+ −
%%% TeX-master: t+ −
%%% End: + −
+ −