| 316 |      1 | % !TEX program = xelatex
 | 
| 66 |      2 | \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
 | 
| 151 |      3 | \usepackage{chessboard}
 | 
|  |      4 | \usepackage[LSBC4,T1]{fontenc}
 | 
| 66 |      5 | \usepackage{../slides}
 | 
|  |      6 | \usepackage{../graphics}
 | 
|  |      7 | \usepackage{../langs}
 | 
| 151 |      8 | 
 | 
| 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 | 
 | 
|  |     27 | 
 | 
|  |     28 | \begin{document}
 | 
|  |     29 | 
 | 
|  |     30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |     31 | \begin{frame}[t]
 | 
|  |     32 | \frametitle{%
 | 
|  |     33 |   \begin{tabular}{@ {}c@ {}}
 | 
|  |     34 |   \\[5mm]
 | 
|  |     35 |   \huge PEP Scala (2) 
 | 
|  |     36 |   \end{tabular}}
 | 
|  |     37 | 
 | 
|  |     38 |   \normalsize
 | 
|  |     39 |   \begin{center}
 | 
|  |     40 |   \begin{tabular}{ll}
 | 
| 147 |     41 |     Email:  & christian.urban at kcl.ac.uk\\
 | 
| 316 |     42 |     Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
 | 
|  |     43 |     Slides \& Code: & KEATS\bigskip\\
 | 
|  |     44 |     Office Hours: &  Thursdays 12:00 -- 14:00\\
 | 
|  |     45 |     Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
 | 
| 66 |     46 |   \end{tabular}
 | 
|  |     47 |   \end{center}
 | 
|  |     48 | 
 | 
|  |     49 | 
 | 
|  |     50 | \end{frame}
 | 
|  |     51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |     52 | 
 | 
| 316 |     53 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |     54 | \begin{frame}[c,fragile]
 | 
|  |     55 |   \frametitle{My Scala Version}
 | 
|  |     56 |   
 | 
|  |     57 |   \begin{lstlisting}[language={},numbers=none,
 | 
|  |     58 |     basicstyle=\ttfamily\small,xleftmargin=-2mm]
 | 
|  |     59 |   $ scala
 | 
|  |     60 |     
 | 
|  |     61 |   Welcome to Scala 2.13.1 (Java HotSpot(TM) 
 | 
|  |     62 |   64-Bit Server VM, Java 9). Type in expressions 
 | 
|  |     63 |   for evaluation. Or try :help.
 | 
|  |     64 |   
 | 
|  |     65 |   scala>
 | 
|  |     66 |   \end{lstlisting}%$
 | 
|  |     67 |   \bigskip\bigskip
 | 
|  |     68 |   
 | 
|  |     69 |   With older versions you will get strange results with my reference implementation.
 | 
|  |     70 |   
 | 
|  |     71 |   \end{frame}
 | 
|  |     72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
| 202 |     73 | 
 | 
| 147 |     74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
| 202 |     75 | \begin{frame}[c,fragile]
 | 
| 316 |     76 | \frametitle{Reference Implementation}
 | 
|  |     77 |   
 | 
|  |     78 | Keep your implementation and my reference implementation separate.\bigskip
 | 
| 147 |     79 | 
 | 
| 316 |     80 |   \begin{lstlisting}[language={},numbers=none,
 | 
|  |     81 |     basicstyle=\ttfamily\small,xleftmargin=-2mm]
 | 
|  |     82 |   $ scala -cp collatz.jar
 | 
| 202 |     83 |   
 | 
| 316 |     84 |   scala> CW6a.collatz(6)
 | 
|  |     85 |   res0: Long = 8
 | 
|  |     86 | 
 | 
| 147 |     87 | 
 | 
| 316 |     88 |   scala> import CW6a._
 | 
|  |     89 |   scala> collatz(9)
 | 
|  |     90 |   res1: Long = 19
 | 
|  |     91 |   \end{lstlisting}%$
 | 
| 202 |     92 | 
 | 
| 147 |     93 | \end{frame}
 | 
|  |     94 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
| 316 |     95 |   
 | 
|  |     96 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |     97 | \begin{frame}[t]
 | 
|  |     98 |   \frametitle{Preliminary Part 7}
 | 
|  |     99 |   
 | 
|  |    100 |   \Large
 | 
|  |    101 |   \[
 | 
|  |    102 |     \texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)}  
 | 
|  |    103 |   \]\bigskip
 | 
|  |    104 | 
 | 
|  |    105 |   \large
 | 
|  |    106 |   \quad{}\;where \;$d_1^2$\; means \;$d_1 \cdot d_1$\; and so on
 | 
|  |    107 | \end{frame}
 | 
|  |    108 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    109 |   
 | 
| 147 |    110 | 
 | 
|  |    111 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |    112 | \begin{frame}[c]
 | 
| 204 |    113 | \frametitle{Assignments}
 | 
| 147 |    114 | 
 | 
| 204 |    115 | Don't change anything with the templates!\bigskip
 | 
|  |    116 | 
 | 
|  |    117 | Avoid at all costs:
 | 
| 147 |    118 | 
 | 
|  |    119 | \begin{itemize}
 | 
| 204 |    120 | \item \texttt{var} 
 | 
|  |    121 | \item \texttt{return} 
 | 
| 147 |    122 | \item \texttt{ListBuffer}
 | 
| 204 |    123 | \item \texttt{mutable}
 | 
|  |    124 | \item \texttt{.par}  
 | 
|  |    125 | \end{itemize}\pause\bigskip
 | 
|  |    126 | 
 | 
|  |    127 | 
 | 
|  |    128 | \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
 | 
|  |    129 | \underline{a}cademic \underline{la}nguage''}\smallskip\\
 | 
|  |    130 | \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
 | 
|  |    131 | \end{frame}
 | 
|  |    132 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    133 | 
 | 
|  |    134 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    135 | \begin{frame}[t]
 | 
|  |    136 | \frametitle{Email: Hate 'val'}
 | 
|  |    137 | 
 | 
|  |    138 | \mbox{}\\[-25mm]\mbox{}
 | 
|  |    139 | 
 | 
|  |    140 | \begin{center}
 | 
|  |    141 |   \begin{bubble}[10.5cm]
 | 
|  |    142 |   Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\
 | 
|  |    143 | 
 | 
|  |    144 |   Hello Mr Urban,\medskip\\
 | 
|  |    145 | 
 | 
|  |    146 |   I just wanted to ask, how are we suppose to work
 | 
|  |    147 |   with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is
 | 
|  |    148 |   this rule active at all? I’ve spent 4 hours not thinking on the
 | 
|  |    149 |   coursework, but how to bypass this annoying rule. What’s the whole
 | 
|  |    150 |   point of all these coursework, when we can’t use everything Scala
 | 
|  |    151 |   gives us?!?\medskip\\
 | 
|  |    152 | 
 | 
|  |    153 |   Regards.\\
 | 
|  |    154 |   \mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\
 | 
|  |    155 |   \end{bubble}
 | 
|  |    156 | \end{center}
 | 
|  |    157 | 
 | 
|  |    158 | \end{frame}
 | 
|  |    159 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |    160 | 
 | 
|  |    161 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    162 | \def\firstcircle{(0,0) circle (1.4cm)}
 | 
|  |    163 | \def\secondcircle{(0:2cm) circle (1.4cm)}
 | 
|  |    164 | \colorlet{circle edge}{blue!50}
 | 
|  |    165 | \colorlet{circle area}{blue!20}
 | 
|  |    166 | 
 | 
|  |    167 | \tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
 | 
|  |    168 |     outline/.style={draw=circle edge, thick}}
 | 
|  |    169 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    170 | \begin{frame}[c,fragile]
 | 
|  |    171 | \frametitle{Par: Intersections}
 | 
|  |    172 | 
 | 
|  |    173 | \begin{textblock}{6}(1,2)
 | 
|  |    174 | \begin{tikzpicture}
 | 
|  |    175 |   \draw[outline] \firstcircle node {$A$};
 | 
|  |    176 |   \node[anchor=south] at (current bounding box.north)
 | 
|  |    177 |     {$A = \{1,2,3,\ldots,1000\}$};
 | 
|  |    178 | \end{tikzpicture}
 | 
|  |    179 | \end{textblock}
 | 
|  |    180 | 
 | 
|  |    181 | \begin{textblock}{6}(8,2)
 | 
|  |    182 | \begin{tikzpicture}
 | 
|  |    183 |   \draw[outline] \secondcircle node {$B$};
 | 
|  |    184 |   \node[anchor=south] at (current bounding box.north)
 | 
|  |    185 |     {$B = \{1,5,9,13,\ldots,997\}$};
 | 
|  |    186 | \end{tikzpicture}
 | 
|  |    187 | \end{textblock}
 | 
|  |    188 | 
 | 
|  |    189 | \begin{textblock}{6}(3.3,9)
 | 
|  |    190 | \begin{tikzpicture}
 | 
|  |    191 |     \begin{scope}
 | 
|  |    192 |         \clip \firstcircle;
 | 
|  |    193 |         \fill[filled] \secondcircle;
 | 
|  |    194 |     \end{scope}
 | 
|  |    195 |     \draw[outline] \firstcircle node {$A$};
 | 
|  |    196 |     \draw[outline] \secondcircle node {$B$};
 | 
|  |    197 |     \node[anchor=north] at (current bounding box.south)
 | 
|  |    198 |     {How many elements are in $A \cap B$?};
 | 
|  |    199 | \end{tikzpicture}
 | 
|  |    200 | \end{textblock}  
 | 
| 147 |    201 | 
 | 
|  |    202 | \end{frame}
 | 
|  |    203 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    204 | 
 | 
|  |    205 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |    206 | \begin{frame}[t]
 | 
|  |    207 | \frametitle{For-Comprehensions Again}
 | 
|  |    208 | 
 | 
|  |    209 | \begin{center}
 | 
|  |    210 |   \begin{tikzpicture}[scale=1,
 | 
|  |    211 |                       node/.style={
 | 
|  |    212 |                       rectangle,rounded corners=3mm,
 | 
|  |    213 |                       very thick,draw=black!50,
 | 
|  |    214 |                       minimum height=18mm, minimum width=20mm,
 | 
|  |    215 |                       top color=white,bottom color=black!20}]
 | 
|  |    216 | 
 | 
|  |    217 |   \node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
 | 
|  |    218 |   \node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
 | 
|  |    219 |   \node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
 | 
|  |    220 |   \node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
 | 
|  |    221 |   \node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
 | 
|  |    222 |   \node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
 | 
|  |    223 |   \node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
 | 
|  |    224 | 
 | 
|  |    225 |   \onslide<2->{
 | 
|  |    226 |   \node (B0) at (1.4,-3) {\texttt{List(}};
 | 
|  |    227 |   \node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
 | 
|  |    228 |   \node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
 | 
|  |    229 |   \node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
 | 
|  |    230 |   \node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
 | 
|  |    231 |   \node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
 | 
|  |    232 | 
 | 
|  |    233 |   \onslide<2->{
 | 
|  |    234 |   \draw [->,line width=1mm] (A1.south) -- (B1.north);
 | 
|  |    235 |   \draw [->,line width=1mm] (A2.south) -- (B2.north);
 | 
|  |    236 |   \draw [->,line width=1mm] (A3.south) -- (B3.north);
 | 
|  |    237 |   \draw [->,line width=1mm] (A4.south) -- (B4.north);
 | 
|  |    238 |   \draw [->,line width=1mm] (A5.south) -- (B5.north);}
 | 
|  |    239 | 
 | 
|  |    240 |   \onslide<2->{
 | 
|  |    241 |   \node (Q1) at (-0.45,-0.1) {};
 | 
|  |    242 |   \node (Q2) at (-0.45,-2.8) {};
 | 
|  |    243 |   \node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
 | 
|  |    244 |   \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
 | 
|  |    245 |  \end{tikzpicture}
 | 
|  |    246 | \end{center}
 | 
|  |    247 | 
 | 
|  |    248 | \onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
 | 
|  |    249 | 
 | 
|  |    250 | \end{frame}
 | 
|  |    251 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    252 | 
 | 
|  |    253 | 
 | 
|  |    254 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
|  |    255 | \begin{frame}[t]
 | 
|  |    256 | \frametitle{For-Comprehensions Again}
 | 
|  |    257 | 
 | 
|  |    258 | \begin{center}
 | 
|  |    259 |   \begin{tikzpicture}[scale=1,
 | 
|  |    260 |                       node/.style={
 | 
|  |    261 |                       rectangle,rounded corners=3mm,
 | 
|  |    262 |                       very thick,draw=black!50,
 | 
|  |    263 |                       minimum height=18mm, minimum width=20mm,
 | 
|  |    264 |                       top color=white,bottom color=black!20}]
 | 
|  |    265 | 
 | 
|  |    266 |   \node (A0) at (0,0)
 | 
|  |    267 |     {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5))
 | 
|  |    268 |              \textcolor{purple}{\textbf{yield}} n\,*\,n}};
 | 
|  |    269 | 
 | 
|  |    270 |   \node (A1) at (0,-1.5) {\LARGE\textbf{vs}};       
 | 
|  |    271 |          
 | 
|  |    272 |   \node (A2) at (0,-3)
 | 
|  |    273 |     {\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5)) println(n)}};
 | 
|  |    274 |  \end{tikzpicture}
 | 
|  |    275 | \end{center}\bigskip
 | 
|  |    276 | 
 | 
|  |    277 | 
 | 
|  |    278 | The second version is in case the for \textbf{does not}
 | 
|  |    279 | produce any result.
 | 
|  |    280 | 
 | 
|  |    281 | \end{frame}
 | 
|  |    282 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
|  |    283 | 
 | 
|  |    284 | 
 | 
|  |    285 | 
 | 
|  |    286 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 | 
| 66 |    287 | \begin{frame}[t]
 | 
| 148 |    288 | \frametitle{Why Scala? No null!}
 | 
| 66 |    289 | 
 | 
|  |    290 | 
 | 
|  |    291 | \begin{itemize}
 | 
|  |    292 | \item \large {\bf You can avoid \textcolor{blue}{\texttt{null}}}:
 | 
|  |    293 | \end{itemize}
 | 
|  |    294 | 
 | 
|  |    295 | 
 | 
|  |    296 | \begin{textblock}{6}(1,5)
 | 
|  |    297 |   \begin{bubble}[10.5cm]\small
 | 
|  |    298 |       ``I call it my billion-dollar mistake. It was the invention of
 | 
|  |    299 |       the null reference in 1965. At that time, I was designing the
 | 
|  |    300 |       first comprehensive type system for references in an object
 | 
|  |    301 |       oriented language (ALGOL W). My goal was to ensure that all use
 | 
|  |    302 |       of references should be absolutely safe, with checking performed
 | 
|  |    303 |       automatically by the compiler. But I couldn't resist the
 | 
|  |    304 |       temptation to put in a null reference, simply because it was so
 | 
|  |    305 |       easy to implement. This has led to innumerable errors,
 | 
|  |    306 |       vulnerabilities, and system crashes, which have probably caused
 | 
|  |    307 |       a billion dollars of pain and damage in the last forty years.''
 | 
|  |    308 |       \hfill Sir Tony (Hoare)
 | 
|  |    309 | \end{bubble}
 | 
|  |    310 | \end{textblock}
 | 
|  |    311 |   
 | 
| 148 |    312 | \begin{textblock}{5}(12.5,1.9)
 | 
|  |    313 | \includegraphics[scale=0.05]{../pics/hoare.jpg}\\
 | 
| 66 |    314 | \end{textblock}
 | 
|  |    315 |   
 | 
|  |    316 | \end{frame}
 | 
| 147 |    317 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
| 66 |    318 | 
 | 
| 148 |    319 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 | 
| 316 |    320 | \begin{frame}[c]
 | 
|  |    321 | \frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
 | 
| 66 |    322 | 
 | 
| 148 |    323 | \begin{center}
 | 
| 316 |    324 |   \begin{tabular}[t]{@{}l@{}l@{}}
 | 
|  |    325 |     \includegraphics[scale=0.1]{../pics/mand4.png} & \hspace{4mm}
 | 
|  |    326 |     \raisebox{0mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}      
 | 
|  |    327 |   \end{tabular}     
 | 
|  |    328 | \end{center}
 | 
|  |    329 | 
 | 
|  |    330 | \begin{center}
 | 
|  |    331 |   My Office Hours: Thursdays 12 -- 14\\
 | 
|  |    332 |   And specifically for Scala: Tuesdays 10:45 -- 11:45
 | 
| 148 |    333 | \end{center}
 | 
| 66 |    334 | \end{frame}
 | 
|  |    335 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 | 
|  |    336 | 
 | 
|  |    337 | 
 | 
|  |    338 | \end{document}
 | 
|  |    339 | 
 | 
|  |    340 | %%% Local Variables:  
 | 
|  |    341 | %%% mode: latex
 | 
|  |    342 | %%% TeX-master: t
 | 
|  |    343 | %%% End: 
 | 
|  |    344 | 
 |