| 
316
 | 
     1  | 
% !TEX program = xelatex
  | 
| 
360
 | 
     2  | 
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
 | 
| 
318
 | 
     3  | 
%\usepackage{chessboard}
 | 
| 
 | 
     4  | 
%\usepackage[LSBC4,T1]{fontenc}
 | 
| 
66
 | 
     5  | 
\usepackage{../slides}
 | 
| 
 | 
     6  | 
\usepackage{../graphics}
 | 
| 
 | 
     7  | 
\usepackage{../langs}
 | 
| 
318
 | 
     8  | 
\usetikzlibrary{shapes}
 | 
| 
148
 | 
     9  | 
% \usepackage{../data}
 | 
| 
66
 | 
    10  | 
  | 
| 
 | 
    11  | 
\hfuzz=220pt 
  | 
| 
 | 
    12  | 
  | 
| 
 | 
    13  | 
%\setmonofont[Scale=.88]{Consolas}
 | 
| 
 | 
    14  | 
%\newfontfamily{\consolas}{Consolas}
 | 
| 
 | 
    15  | 
  | 
| 
 | 
    16  | 
\lstset{language=Scala,
 | 
| 
 | 
    17  | 
        style=mystyle,
  | 
| 
 | 
    18  | 
        numbersep=0pt,
  | 
| 
 | 
    19  | 
        numbers=none,
  | 
| 
 | 
    20  | 
        xleftmargin=0mm}
  | 
| 
 | 
    21  | 
  | 
| 
 | 
    22  | 
\newcommand{\bl}[1]{\textcolor{blue}{#1}}     
 | 
| 
 | 
    23  | 
  | 
| 
 | 
    24  | 
% beamer stuff 
  | 
| 
 | 
    25  | 
\renewcommand{\slidecaption}{PEP (Scala) 02, King's College London}
 | 
| 
 | 
    26  | 
  | 
| 
318
 | 
    27  | 
\newcommand{\UParrow}[3]{%
 | 
| 
 | 
    28  | 
\begin{textblock}{0}(#2,#3)%
 | 
| 
 | 
    29  | 
\onslide<#1>{%
 | 
| 
 | 
    30  | 
\begin{tikzpicture}%
 | 
| 
 | 
    31  | 
\node at (0,0) [single arrow, shape border rotate=90, fill=red,text=red]{a};%
 | 
| 
 | 
    32  | 
\end{tikzpicture}}%
 | 
| 
 | 
    33  | 
\end{textblock}}
 | 
| 
66
 | 
    34  | 
  | 
| 
 | 
    35  | 
\begin{document}
 | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
    38  | 
\begin{frame}[t]
 | 
| 
 | 
    39  | 
\frametitle{%
 | 
| 
 | 
    40  | 
  \begin{tabular}{@ {}c@ {}}
 | 
| 
 | 
    41  | 
  \\[5mm]
  | 
| 
 | 
    42  | 
  \huge PEP Scala (2) 
  | 
| 
 | 
    43  | 
  \end{tabular}}
 | 
| 
 | 
    44  | 
  | 
| 
 | 
    45  | 
  \normalsize
  | 
| 
 | 
    46  | 
  \begin{center}
 | 
| 
 | 
    47  | 
  \begin{tabular}{ll}
 | 
| 
147
 | 
    48  | 
    Email:  & christian.urban at kcl.ac.uk\\
  | 
| 
360
 | 
    49  | 
    %Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
 | 
| 
316
 | 
    50  | 
    Slides \& Code: & KEATS\bigskip\\
  | 
| 
360
 | 
    51  | 
    %Office Hours: &  Thursdays 12:00 -- 14:00\\
  | 
| 
 | 
    52  | 
    %Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
  | 
| 
66
 | 
    53  | 
  \end{tabular}
 | 
| 
 | 
    54  | 
  \end{center}
 | 
| 
 | 
    55  | 
  | 
| 
 | 
    56  | 
  | 
| 
 | 
    57  | 
\end{frame}
 | 
| 
 | 
    58  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
    59  | 
  | 
| 
202
 | 
    60  | 
  | 
| 
362
 | 
    61  | 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
    62  | 
% \begin{frame}[t,fragile]
 | 
| 
 | 
    63  | 
% \frametitle{For-Comprehensions}
 | 
| 
202
 | 
    64  | 
  | 
| 
362
 | 
    65  | 
% %\small
  | 
| 
 | 
    66  | 
% \begin{lstlisting}[language=Scala,numbers=none]
 | 
| 
 | 
    67  | 
% for (n <- List(1, 2, 3, 4, 5)) yield n * n
  | 
| 
 | 
    68  | 
% \end{lstlisting}
 | 
| 
147
 | 
    69  | 
  | 
| 
362
 | 
    70  | 
% \begin{textblock}{5}(2,6)
 | 
| 
 | 
    71  | 
% \includegraphics[scale=0.3]{../pics/fun.png}
 | 
| 
 | 
    72  | 
% \end{textblock}  
 | 
| 
204
 | 
    73  | 
  | 
| 
362
 | 
    74  | 
% \begin{textblock}{5}(9,6)
 | 
| 
 | 
    75  | 
% \includegraphics[scale=0.3]{../pics/fun.png}
 | 
| 
 | 
    76  | 
% \end{textblock}  
 | 
| 
204
 | 
    77  | 
  | 
| 
 | 
    78  | 
  | 
| 
362
 | 
    79  | 
% \end{frame}
 | 
| 
204
 | 
    80  | 
  | 
| 
147
 | 
    81  | 
  | 
| 
363
 | 
    82  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
    83  | 
\begin{frame}[t]
 | 
| 
 | 
    84  | 
\frametitle{For-Comprehensions}
 | 
| 
147
 | 
    85  | 
  | 
| 
363
 | 
    86  | 
\begin{center}
 | 
| 
 | 
    87  | 
  \begin{tikzpicture}[scale=1,
 | 
| 
 | 
    88  | 
                      node/.style={
 | 
| 
 | 
    89  | 
                      rectangle,rounded corners=3mm,
  | 
| 
 | 
    90  | 
                      very thick,draw=black!50,
  | 
| 
 | 
    91  | 
                      minimum height=18mm, minimum width=20mm,
  | 
| 
 | 
    92  | 
                      top color=white,bottom color=black!20}]
  | 
| 
147
 | 
    93  | 
  | 
| 
363
 | 
    94  | 
  \node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
 | 
| 
 | 
    95  | 
  \node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
 | 
| 
 | 
    96  | 
  \node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
 | 
| 
 | 
    97  | 
  \node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
 | 
| 
 | 
    98  | 
  \node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
 | 
| 
 | 
    99  | 
  \node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
 | 
| 
 | 
   100  | 
  \node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
 | 
| 
147
 | 
   101  | 
  | 
| 
363
 | 
   102  | 
  \onslide<2->{
 | 
| 
 | 
   103  | 
  \node (B0) at (1.4,-3) {\texttt{List(}};
 | 
| 
 | 
   104  | 
  \node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
 | 
| 
 | 
   105  | 
  \node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
 | 
| 
 | 
   106  | 
  \node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
 | 
| 
 | 
   107  | 
  \node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
 | 
| 
 | 
   108  | 
  \node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
 | 
| 
147
 | 
   109  | 
  | 
| 
363
 | 
   110  | 
  \onslide<2->{
 | 
| 
 | 
   111  | 
  \draw [->,line width=1mm] (A1.south) -- (B1.north);
  | 
| 
 | 
   112  | 
  \draw [->,line width=1mm] (A2.south) -- (B2.north);
  | 
| 
 | 
   113  | 
  \draw [->,line width=1mm] (A3.south) -- (B3.north);
  | 
| 
 | 
   114  | 
  \draw [->,line width=1mm] (A4.south) -- (B4.north);
  | 
| 
 | 
   115  | 
  \draw [->,line width=1mm] (A5.south) -- (B5.north);}
  | 
| 
147
 | 
   116  | 
  | 
| 
363
 | 
   117  | 
  \onslide<2->{
 | 
| 
 | 
   118  | 
  \node (Q1) at (-0.45,-0.1) {};
 | 
| 
 | 
   119  | 
  \node (Q2) at (-0.45,-2.8) {};
 | 
| 
 | 
   120  | 
  \node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
 | 
| 
 | 
   121  | 
  \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
  | 
| 
 | 
   122  | 
 \end{tikzpicture}
 | 
| 
 | 
   123  | 
\end{center}
 | 
| 
147
 | 
   124  | 
  | 
| 
363
 | 
   125  | 
\onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
 | 
| 
147
 | 
   126  | 
  | 
| 
363
 | 
   127  | 
\end{frame}
 | 
| 
 | 
   128  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
147
 | 
   129  | 
  | 
| 
 | 
   130  | 
  | 
| 
362
 | 
   131  | 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   132  | 
% \begin{frame}[t]
 | 
| 
 | 
   133  | 
% \frametitle{For-Comprehensions Again}
 | 
| 
147
 | 
   134  | 
  | 
| 
362
 | 
   135  | 
% \begin{center}
 | 
| 
 | 
   136  | 
%   \begin{tikzpicture}[scale=1,
 | 
| 
 | 
   137  | 
%                       node/.style={
 | 
| 
 | 
   138  | 
%                       rectangle,rounded corners=3mm,
  | 
| 
 | 
   139  | 
%                       very thick,draw=black!50,
  | 
| 
 | 
   140  | 
%                       minimum height=18mm, minimum width=20mm,
  | 
| 
 | 
   141  | 
%                       top color=white,bottom color=black!20}]
  | 
| 
147
 | 
   142  | 
  | 
| 
362
 | 
   143  | 
%   \node (A0) at (0,0)
  | 
| 
 | 
   144  | 
%     {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5))
 | 
| 
 | 
   145  | 
%              \textcolor{purple}{\textbf{yield}} n\,*\,n}};
 | 
| 
147
 | 
   146  | 
  | 
| 
362
 | 
   147  | 
%   \node (A1) at (0,-1.5) {\LARGE\textbf{vs}};       
 | 
| 
147
 | 
   148  | 
         
  | 
| 
362
 | 
   149  | 
%   \node (A2) at (0,-3)
  | 
| 
 | 
   150  | 
%     {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5)) println(n)}};
 | 
| 
 | 
   151  | 
%  \end{tikzpicture}
 | 
| 
 | 
   152  | 
% \end{center}\bigskip
 | 
| 
147
 | 
   153  | 
  | 
| 
 | 
   154  | 
  | 
| 
362
 | 
   155  | 
% The second version is in case the for \textbf{does not}
 | 
| 
 | 
   156  | 
% produce any result.
  | 
| 
147
 | 
   157  | 
  | 
| 
362
 | 
   158  | 
% \end{frame}
 | 
| 
 | 
   159  | 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
147
 | 
   160  | 
  | 
| 
 | 
   161  | 
  | 
| 
 | 
   162  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
318
 | 
   163  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   164  | 
  %\frametitle{Option Type}
 | 
| 
 | 
   165  | 
  
  | 
| 
 | 
   166  | 
  Find something below 4 in a list. What do you think Scala answers?\bigskip\bigskip
  | 
| 
 | 
   167  | 
  
  | 
| 
 | 
   168  | 
  \begin{onlyenv}<1>
 | 
| 
 | 
   169  | 
  \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
 | 
| 
 | 
   170  | 
  List(7,2,3,4,5,6).find(_ < 4)
  | 
| 
 | 
   171  | 
       
  | 
| 
 | 
   172  | 
  List(5,6,7,8,9).find(_ < 4)
  | 
| 
 | 
   173  | 
  \end{lstlisting}
 | 
| 
 | 
   174  | 
  \end{onlyenv}
 | 
| 
 | 
   175  | 
  \begin{onlyenv}<2>
 | 
| 
 | 
   176  | 
  \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
 | 
| 
 | 
   177  | 
  List(7,2,3,4,5,6).find(_ < 4)
  | 
| 
 | 
   178  | 
  res: Option[Int] = Some(2)
  | 
| 
 | 
   179  | 
   
  | 
| 
 | 
   180  | 
  List(5,6,7,8,9).find(_ < 4)
  | 
| 
 | 
   181  | 
  res: Option[Int] = None
  | 
| 
 | 
   182  | 
  \end{lstlisting}
 | 
| 
 | 
   183  | 
  \end{onlyenv}
 | 
| 
 | 
   184  | 
  
  | 
| 
 | 
   185  | 
  \end{frame}
 | 
| 
 | 
   186  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   187  | 
 
  | 
| 
 | 
   188  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   189  | 
\begin{frame}[c]
 | 
| 
 | 
   190  | 
\frametitle{Option Type}
 | 
| 
 | 
   191  | 
    
  | 
| 
 | 
   192  | 
\begin{itemize}
 | 
| 
 | 
   193  | 
\item if the value is present, you use\bigskip
  | 
| 
 | 
   194  | 
\begin{center}\pcode{Some(value)}\end{center}\bigskip\bigskip
 | 
| 
 | 
   195  | 
  | 
| 
 | 
   196  | 
\item if no value is present, you use\bigskip
  | 
| 
 | 
   197  | 
\begin{center}\pcode{None}\end{center}\bigskip\bigskip
 | 
| 
 | 
   198  | 
\end{itemize}
 | 
| 
 | 
   199  | 
  | 
| 
 | 
   200  | 
\small e.g.~\code{Option[Int]}, then \code{Some(42)} and \code{None}\\
 | 
| 
 | 
   201  | 
good for error handling      
  | 
| 
 | 
   202  | 
\end{frame}
 | 
| 
 | 
   203  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   204  | 
    
  | 
| 
 | 
   205  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   206  | 
\begin{frame}[c,fragile]
 | 
| 
317
 | 
   207  | 
\frametitle{Option Type}
 | 
| 
66
 | 
   208  | 
  | 
| 
318
 | 
   209  | 
\small   
  | 
| 
 | 
   210  | 
\begin{onlyenv}<1>
 | 
| 
 | 
   211  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
 | 
| 
 | 
   212  | 
Integer.parseInt("1234")
 | 
| 
66
 | 
   213  | 
  | 
| 
318
 | 
   214  | 
// vs.
  | 
| 
 | 
   215  | 
  | 
| 
 | 
   216  | 
def get_me_an_int(s: String) : Option[Int] = 
  | 
| 
 | 
   217  | 
 Try(Some(Integer.parseInt(s))).getOrElse(None)
  | 
| 
 | 
   218  | 
\end{lstlisting}
 | 
| 
 | 
   219  | 
\end{onlyenv}\bigskip\bigskip\bigskip
 | 
| 
 | 
   220  | 
 
  | 
| 
319
 | 
   221  | 
in the Scala code it is clear from the type I that have to deal 
  | 
| 
318
 | 
   222  | 
with the \pcode{None}-case; no JavaDoc needed
 | 
| 
66
 | 
   223  | 
  
  | 
| 
 | 
   224  | 
\end{frame}
 | 
| 
147
 | 
   225  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
318
 | 
   226  | 
 
  | 
| 
66
 | 
   227  | 
  | 
| 
317
 | 
   228  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
318
 | 
   229  | 
\begin{frame}[c,fragile]
 | 
| 
317
 | 
   230  | 
\frametitle{Higher-Order Functions}
 | 
| 
 | 
   231  | 
  
  | 
| 
318
 | 
   232  | 
In Scala, functions can take other functions as arguments and can return 
  | 
| 
 | 
   233  | 
a function as a result.\bigskip\bigskip  
  | 
| 
 | 
   234  | 
  | 
| 
 | 
   235  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   236  | 
List(7,2,3,4,5,6).find(_ < 4)
  | 
| 
 | 
   237  | 
\end{lstlisting}
 | 
| 
 | 
   238  | 
    
  | 
| 
362
 | 
   239  | 
\UParrow{1}{8}{11}    
 | 
| 
318
 | 
   240  | 
\end{frame}
 | 
| 
 | 
   241  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
317
 | 
   242  | 
  
  | 
| 
318
 | 
   243  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   244  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   245  | 
\frametitle{Higher-Order Functions (2)}
 | 
| 
 | 
   246  | 
  
  | 
| 
 | 
   247  | 
  | 
| 
 | 
   248  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   249  | 
def even(x: Int) : Boolean = x % 2 == 0
  | 
| 
 | 
   250  | 
  | 
| 
 | 
   251  | 
List(1, 2, 3, 4, 5).filter(even)
  | 
| 
 | 
   252  | 
  res : List[Int] = List(2, 4)
  | 
| 
 | 
   253  | 
  | 
| 
 | 
   254  | 
List(1, 2, 3, 4, 5).count(even)
  | 
| 
 | 
   255  | 
  res : Int = 2
  | 
| 
 | 
   256  | 
  | 
| 
 | 
   257  | 
List(1, 2, 3, 4, 5).find(even)
  | 
| 
 | 
   258  | 
  res: Option[Int] = Some(2)
  | 
| 
 | 
   259  | 
\end{lstlisting}
 | 
| 
317
 | 
   260  | 
    
  | 
| 
 | 
   261  | 
\end{frame}
 | 
| 
 | 
   262  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
362
 | 
   263  | 
  | 
| 
 | 
   264  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   265  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   266  | 
\frametitle{Anonymous Functions}
 | 
| 
 | 
   267  | 
  
  | 
| 
 | 
   268  | 
  | 
| 
 | 
   269  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   270  | 
def less4(x: Int) = x < 4
  | 
| 
 | 
   271  | 
\end{lstlisting}
 | 
| 
 | 
   272  | 
  | 
| 
 | 
   273  | 
\begin{center}
 | 
| 
 | 
   274  | 
vs
  | 
| 
 | 
   275  | 
\end{center}  
 | 
| 
 | 
   276  | 
  | 
| 
 | 
   277  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   278  | 
      (x: Int) => x < 4
  | 
| 
 | 
   279  | 
\end{lstlisting}
 | 
| 
 | 
   280  | 
  | 
| 
 | 
   281  | 
\end{frame}
 | 
| 
 | 
   282  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
318
 | 
   283  | 
    
  | 
| 
 | 
   284  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   285  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   286  | 
\frametitle{map (lower case)}
 | 
| 
 | 
   287  | 
  | 
| 
 | 
   288  | 
 applies a function to each element of a list (and more)
  | 
| 
 | 
   289  | 
  | 
| 
 | 
   290  | 
\begin{center}
 | 
| 
 | 
   291  | 
\begin{tikzpicture}[scale=0.9]
 | 
| 
 | 
   292  | 
                      
  | 
| 
 | 
   293  | 
  \node (A0) at (1.2,0) {\texttt{List(\,}};
 | 
| 
 | 
   294  | 
  \node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
 | 
| 
 | 
   295  | 
  \node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
 | 
| 
 | 
   296  | 
  \node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
 | 
| 
 | 
   297  | 
  \node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
 | 
| 
 | 
   298  | 
  \node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
 | 
| 
 | 
   299  | 
  \node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
 | 
| 
 | 
   300  | 
  \node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
 | 
| 
 | 
   301  | 
  \node (A8) at (8.3,0) {\texttt{8)}};
 | 
| 
 | 
   302  | 
  | 
| 
 | 
   303  | 
  \node (B0) at (1.2,-3) {\texttt{List(\,}};
 | 
| 
 | 
   304  | 
  \node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
 | 
| 
 | 
   305  | 
  \node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
 | 
| 
 | 
   306  | 
  \node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
 | 
| 
 | 
   307  | 
  \node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
 | 
| 
 | 
   308  | 
  \node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
 | 
| 
 | 
   309  | 
  \node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
 | 
| 
 | 
   310  | 
  \node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
 | 
| 
 | 
   311  | 
  \node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
 | 
| 
 | 
   312  | 
  | 
| 
 | 
   313  | 
  \draw [->,line width=1mm] (A1.south) -- (B1.north);
  | 
| 
 | 
   314  | 
  \draw [->,line width=1mm] (A2.south) -- (B2.north);
  | 
| 
 | 
   315  | 
  \draw [->,line width=1mm] (A3.south) -- (B3.north);
  | 
| 
 | 
   316  | 
  \draw [->,line width=1mm] (A4.south) -- (B4.north);
  | 
| 
 | 
   317  | 
  \draw [->,line width=1mm] (A5.south) -- (B5.north);
  | 
| 
 | 
   318  | 
  \draw [->,line width=1mm] (A6.south) -- (B6.north);
  | 
| 
 | 
   319  | 
  \draw [->,line width=1mm] (A7.south) -- (B7.north);
  | 
| 
 | 
   320  | 
  \draw [->,line width=1mm] (A8.south) -- (B8.north);
  | 
| 
 | 
   321  | 
  | 
| 
 | 
   322  | 
  \node [red] (Q0) at (-0.5,-0.3) {\large\texttt{n}}; 
 | 
| 
 | 
   323  | 
  \node (Q1) at (-0.5,-0.4) {};
 | 
| 
 | 
   324  | 
  \node (Q2) at (-0.5,-2.5) {};
 | 
| 
 | 
   325  | 
  \node [red] (Q3) at (-0.5,-2.65) {\large\texttt{n\,*\,n}};
 | 
| 
 | 
   326  | 
  \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);
  | 
| 
 | 
   327  | 
  | 
| 
 | 
   328  | 
  \node [red] at (-1.5,-1.5) {\Large{}\it\textbf{map}};
 | 
| 
 | 
   329  | 
 \end{tikzpicture}
 | 
| 
 | 
   330  | 
\end{center}\bigskip
 | 
| 
 | 
   331  | 
  | 
| 
 | 
   332  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   333  | 
List(1,2,3,4,5,6,7,8).map(n => n * n)
  | 
| 
 | 
   334  | 
\end{lstlisting}
 | 
| 
 | 
   335  | 
\end{frame}
 | 
| 
 | 
   336  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
317
 | 
   337  | 
  
  | 
| 
318
 | 
   338  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   339  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   340  | 
\frametitle{For-Comprehensions are maps}
 | 
| 
 | 
   341  | 
  
  | 
| 
 | 
   342  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
 | 
| 
 | 
   343  | 
for (n <- List(1,2,3,4,5,6,7,8)) 
  | 
| 
 | 
   344  | 
  yield n * n
  | 
| 
 | 
   345  | 
  | 
| 
 | 
   346  | 
  | 
| 
 | 
   347  | 
// is just syntactic sugar for
  | 
| 
 | 
   348  | 
  | 
| 
 | 
   349  | 
  | 
| 
 | 
   350  | 
List(1,2,3,4,5,6,7,8).map(n => n * n)
  | 
| 
 | 
   351  | 
\end{lstlisting}
 | 
| 
 | 
   352  | 
    
  | 
| 
 | 
   353  | 
\end{frame}
 | 
| 
 | 
   354  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   355  | 
    
  | 
| 
 | 
   356  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   357  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   358  | 
\frametitle{Map (upper case)}
 | 
| 
 | 
   359  | 
  | 
| 
 | 
   360  | 
a type, representing a key-value association datastructure\bigskip\bigskip
  | 
| 
 | 
   361  | 
  | 
| 
 | 
   362  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
 | 
| 
 | 
   363  | 
val ascii = 
  | 
| 
 | 
   364  | 
     ('a' to 'z').map(c => (c, c.toInt))
 | 
| 
 | 
   365  | 
  | 
| 
 | 
   366  | 
val ascii_Map = ascii.toMap
  | 
| 
 | 
   367  | 
  | 
| 
 | 
   368  | 
ascii_Map.get('a')   // -> 97
 | 
| 
 | 
   369  | 
\end{lstlisting}
 | 
| 
 | 
   370  | 
\end{frame}
 | 
| 
 | 
   371  | 
  | 
| 
 | 
   372  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   373  | 
\begin{frame}[c,fragile]
 | 
| 
319
 | 
   374  | 
\frametitle{Pattern Matching}
 | 
| 
 | 
   375  | 
  | 
| 
 | 
   376  | 
\ldots on pairs:\bigskip
  | 
| 
318
 | 
   377  | 
  | 
| 
319
 | 
   378  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
 | 
| 
 | 
   379  | 
def fizz_buzz(n: Int) : String = 
  | 
| 
 | 
   380  | 
 (n % 3, n % 5) match {
 | 
| 
 | 
   381  | 
   case (0, 0) => "fizz buzz"
  | 
| 
 | 
   382  | 
   case (0, _) => "fizz"
  | 
| 
 | 
   383  | 
   case (_, 0) => "buzz"
  | 
| 
 | 
   384  | 
   case _ => n.toString  
  | 
| 
 | 
   385  | 
 }
  | 
| 
 | 
   386  | 
\end{lstlisting}
 | 
| 
 | 
   387  | 
\end{frame}
 | 
| 
318
 | 
   388  | 
  | 
| 
319
 | 
   389  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   390  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   391  | 
\frametitle{Recursion}
 | 
| 
318
 | 
   392  | 
  | 
| 
 | 
   393  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
 | 
| 
 | 
   394  | 
def fib(n: Int) : Int = { 
 | 
| 
 | 
   395  | 
  if (n == 0 || n == 1) 1
  | 
| 
 | 
   396  | 
   else fib(n - 1) + fib(n - 2)
  | 
| 
 | 
   397  | 
}
  | 
| 
 | 
   398  | 
\end{lstlisting}
 | 
| 
 | 
   399  | 
\end{frame}
 | 
| 
 | 
   400  | 
  | 
| 
 | 
   401  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   402  | 
\begin{frame}[c,fragile]
 | 
| 
 | 
   403  | 
\frametitle{Recursion}
 | 
| 
 | 
   404  | 
  | 
| 
 | 
   405  | 
\small
  | 
| 
 | 
   406  | 
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-4mm]
 | 
| 
 | 
   407  | 
def my_flatten(xs: List[Option[Int]]): List[Int] = 
  | 
| 
 | 
   408  | 
 xs match {
 | 
| 
 | 
   409  | 
   case Nil => Nil 
  | 
| 
 | 
   410  | 
   case None :: rest => my_flatten(rest)
  | 
| 
 | 
   411  | 
   case Some(v) :: rest => v :: my_flatten(rest)
  | 
| 
 | 
   412  | 
 }
  | 
| 
 | 
   413  | 
\end{lstlisting}
 | 
| 
 | 
   414  | 
\end{frame}
 | 
| 
 | 
   415  | 
  | 
| 
 | 
   416  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
317
 | 
   417  | 
  | 
| 
148
 | 
   418  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
316
 | 
   419  | 
\begin{frame}[c]
 | 
| 
 | 
   420  | 
\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
 | 
| 
66
 | 
   421  | 
  | 
| 
148
 | 
   422  | 
\begin{center}
 | 
| 
316
 | 
   423  | 
  \begin{tabular}[t]{@{}l@{}l@{}}
 | 
| 
 | 
   424  | 
    \includegraphics[scale=0.1]{../pics/mand4.png} & \hspace{4mm}
 | 
| 
 | 
   425  | 
    \raisebox{0mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}      
 | 
| 
 | 
   426  | 
  \end{tabular}     
 | 
| 
 | 
   427  | 
\end{center}
 | 
| 
 | 
   428  | 
  | 
| 
 | 
   429  | 
\begin{center}
 | 
| 
 | 
   430  | 
  My Office Hours: Thursdays 12 -- 14\\
  | 
| 
 | 
   431  | 
  And specifically for Scala: Tuesdays 10:45 -- 11:45
  | 
| 
148
 | 
   432  | 
\end{center}
 | 
| 
66
 | 
   433  | 
\end{frame}
 | 
| 
 | 
   434  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
  | 
| 
 | 
   435  | 
  | 
| 
 | 
   436  | 
  | 
| 
318
 | 
   437  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   438  | 
\begin{frame}[t]
 | 
| 
 | 
   439  | 
  | 
| 
 | 
   440  | 
  \begin{center}  
 | 
| 
 | 
   441  | 
  \includegraphics[scale=0.3]{../pics/blow.png}
 | 
| 
 | 
   442  | 
  \end{center}
 | 
| 
 | 
   443  | 
  
  | 
| 
 | 
   444  | 
  \begin{textblock}{14}(2,11.4)
 | 
| 
 | 
   445  | 
  \large\bf{}Mind-Blowing Programming Languages:\\ 
 | 
| 
 | 
   446  | 
  Overloading in any language is great but it makes a difference\; \code{10/3}
 | 
| 
 | 
   447  | 
  \;or\; \code{10.0/3}
 | 
| 
 | 
   448  | 
  \end{textblock}
 | 
| 
 | 
   449  | 
  \end{frame}
 | 
| 
 | 
   450  | 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   451  | 
    
  | 
| 
 | 
   452  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   453  | 
\begin{frame}[t]
 | 
| 
 | 
   454  | 
  | 
| 
 | 
   455  | 
  \begin{center}  
 | 
| 
 | 
   456  | 
  \includegraphics[scale=0.3]{../pics/blow.png}
 | 
| 
 | 
   457  | 
  \end{center}
 | 
| 
 | 
   458  | 
  
  | 
| 
 | 
   459  | 
  \begin{textblock}{14}(2,11.4)
 | 
| 
 | 
   460  | 
  \large\bf{}Mind-Blowing Programming Languages:\\ 
 | 
| 
319
 | 
   461  | 
  \centering PHP \textcolor{gray}{(7.0)}
 | 
| 
318
 | 
   462  | 
  \end{textblock}
 | 
| 
 | 
   463  | 
  \end{frame}
 | 
| 
 | 
   464  | 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   465  | 
  | 
| 
319
 | 
   466  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   467  | 
  | 
| 
 | 
   468  | 
\begin{frame}[c]
 | 
| 
 | 
   469  | 
\frametitle{Jumping Towers}
 | 
| 
 | 
   470  | 
  | 
| 
 | 
   471  | 
\begin{center}
 | 
| 
 | 
   472  | 
\begin{tikzpicture}[scale=1.2]
 | 
| 
 | 
   473  | 
  \draw[line width=1mm,cap=round] (0,0) -- (5,0);
  | 
| 
 | 
   474  | 
  \draw[line width=1mm,cap=round] (0,1) -- (5,1);
  | 
| 
 | 
   475  | 
  | 
| 
 | 
   476  | 
  \draw[line width=1mm,cap=round] (0,0) -- (0,1);
  | 
| 
 | 
   477  | 
  \node at (0.5,0.5) {\textbf{\Large 3}};
 | 
| 
 | 
   478  | 
  | 
| 
 | 
   479  | 
  \draw[line width=1mm,cap=round] (1,0) -- (1,1);
  | 
| 
 | 
   480  | 
  \node at (1.5,0.5) {\textbf{\Large 4}};
 | 
| 
 | 
   481  | 
  | 
| 
 | 
   482  | 
  \draw[line width=1mm,cap=round] (2,0) -- (2,1);
  | 
| 
 | 
   483  | 
  \node at (2.5,0.5) {\textbf{\Large 2}};
 | 
| 
 | 
   484  | 
  | 
| 
 | 
   485  | 
  \draw[line width=1mm,cap=round] (3,0) -- (3,1);
  | 
| 
 | 
   486  | 
  \node at (3.5,0.5) {\textbf{\Large 0}};
 | 
| 
 | 
   487  | 
  
  | 
| 
 | 
   488  | 
  \draw[line width=1mm,cap=round] (4,0) -- (4,1);
  | 
| 
 | 
   489  | 
  | 
| 
 | 
   490  | 
  \node at (4.5,0.5) {\textbf{\Large 1}};
 | 
| 
 | 
   491  | 
  
  | 
| 
 | 
   492  | 
  \draw[line width=1mm,cap=round] (5,0) -- (5,1);
  | 
| 
 | 
   493  | 
  | 
| 
 | 
   494  | 
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (1.5,1);
  | 
| 
 | 
   495  | 
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (2.5,1);
  | 
| 
 | 
   496  | 
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (3.5,1);
  | 
| 
 | 
   497  | 
  | 
| 
 | 
   498  | 
  \draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (3.5,0);
  | 
| 
 | 
   499  | 
  \draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (4.5,0);
  | 
| 
 | 
   500  | 
  | 
| 
 | 
   501  | 
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (4.5,1) to (5.7,1);
  | 
| 
 | 
   502  | 
  \node at (5.7, 0.8) {End};
 | 
| 
 | 
   503  | 
\end{tikzpicture}
 | 
| 
 | 
   504  | 
\end{center}\bigskip
 | 
| 
 | 
   505  | 
  | 
| 
 | 
   506  | 
  | 
| 
 | 
   507  | 
shortest: 3 $\rightarrow$ 4 $\rightarrow$ End
  | 
| 
 | 
   508  | 
  | 
| 
 | 
   509  | 
\end{frame}
 | 
| 
 | 
   510  | 
  | 
| 
 | 
   511  | 
  | 
| 
 | 
   512  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
 | 
   513  | 
  | 
| 
 | 
   514  | 
  | 
| 
 | 
   515  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
  | 
| 
 | 
   516  | 
  | 
| 
 | 
   517  | 
\begin{frame}[c]
 | 
| 
 | 
   518  | 
\frametitle{``Children'' / moves}
 | 
| 
 | 
   519  | 
  | 
| 
 | 
   520  | 
\begin{center}
 | 
| 
 | 
   521  | 
  \begin{tikzpicture}
 | 
| 
 | 
   522  | 
    [grow=right,level distance=30mm,child anchor=north,line width=0.5mm]
  | 
| 
 | 
   523  | 
  \node {$[3,4,2,0,1]$}
 | 
| 
 | 
   524  | 
     child {node {$[0,1]$}}
 | 
| 
 | 
   525  | 
     child {node {$[2,0,1]$}
 | 
| 
 | 
   526  | 
        child {node {$[1]$} child [level distance=13mm] {node {End}}}
 | 
| 
 | 
   527  | 
        child {node {$[0,1]$}}
 | 
| 
 | 
   528  | 
     }
  | 
| 
 | 
   529  | 
     child {node {$[4,2,0,1]$\ldots}};
 | 
| 
 | 
   530  | 
\end{tikzpicture}
 | 
| 
 | 
   531  | 
\end{center}
 | 
| 
363
 | 
   532  | 
\end{frame}
 | 
| 
319
 | 
   533  | 
  | 
| 
363
 | 
   534  | 
\begin{frame}<1-10>[t]
 | 
| 
 | 
   535  | 
  
  | 
| 
 | 
   536  | 
\end{frame}
 | 
| 
319
 | 
   537  | 
  | 
| 
 | 
   538  | 
  | 
| 
 | 
   539  | 
  | 
| 
 | 
   540  | 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  | 
| 
318
 | 
   541  | 
  | 
| 
66
 | 
   542  | 
\end{document}
 | 
| 
 | 
   543  | 
  | 
| 
 | 
   544  | 
%%% Local Variables:  
  | 
| 
 | 
   545  | 
%%% mode: latex
  | 
| 
 | 
   546  | 
%%% TeX-master: t
  | 
| 
 | 
   547  | 
%%% End: 
  | 
| 
 | 
   548  | 
  |