--- a/slides/slides02.tex Mon Nov 11 14:04:22 2019 +0000
+++ b/slides/slides02.tex Tue Nov 12 00:41:00 2019 +0000
@@ -1,11 +1,11 @@
% !TEX program = xelatex
\documentclass[dvipsnames,14pt,t,xelatex]{beamer}
-\usepackage{chessboard}
-\usepackage[LSBC4,T1]{fontenc}
+%\usepackage{chessboard}
+%\usepackage[LSBC4,T1]{fontenc}
\usepackage{../slides}
\usepackage{../graphics}
\usepackage{../langs}
-
+\usetikzlibrary{shapes}
% \usepackage{../data}
\hfuzz=220pt
@@ -24,6 +24,13 @@
% 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}
@@ -52,7 +59,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
- \frametitle{My Scala Version}
+ \frametitle{Scala 2.13.1}
\begin{lstlisting}[language={},numbers=none,
basicstyle=\ttfamily\small,xleftmargin=-2mm]
@@ -112,7 +119,7 @@
\frametitle{Discussion Forum}
\large
- ``Since we cant use \code{var}s I was wondering if we could use a stack?''
+ ``Since we can't use \code{var}s I was wondering if we could use a stack?''
\bigskip\bigskip\bigskip\bigskip
\small
@@ -375,25 +382,226 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
+\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 have to deal
+with the \pcode{None}-case; no JavaDoc needed
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
+\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{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]
@@ -414,6 +622,36 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\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
+ \end{textblock}
+ \end{frame}
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
\end{document}
%%% Local Variables: