316
|
1 |
% !TEX program = xelatex
|
66
|
2 |
\documentclass[dvipsnames,14pt,t,xelatex]{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\\
|
316
|
49 |
Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
|
|
50 |
Slides \& Code: & KEATS\bigskip\\
|
|
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 |
|
316
|
60 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
61 |
\begin{frame}[c,fragile]
|
318
|
62 |
\frametitle{Scala 2.13.1}
|
316
|
63 |
|
|
64 |
\begin{lstlisting}[language={},numbers=none,
|
|
65 |
basicstyle=\ttfamily\small,xleftmargin=-2mm]
|
|
66 |
$ scala
|
|
67 |
|
|
68 |
Welcome to Scala 2.13.1 (Java HotSpot(TM)
|
|
69 |
64-Bit Server VM, Java 9). Type in expressions
|
|
70 |
for evaluation. Or try :help.
|
|
71 |
|
|
72 |
scala>
|
|
73 |
\end{lstlisting}%$
|
|
74 |
\bigskip\bigskip
|
|
75 |
|
|
76 |
With older versions you will get strange results with my reference implementation.
|
|
77 |
|
|
78 |
\end{frame}
|
|
79 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
202
|
80 |
|
147
|
81 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
202
|
82 |
\begin{frame}[c,fragile]
|
316
|
83 |
\frametitle{Reference Implementation}
|
|
84 |
|
|
85 |
Keep your implementation and my reference implementation separate.\bigskip
|
147
|
86 |
|
316
|
87 |
\begin{lstlisting}[language={},numbers=none,
|
|
88 |
basicstyle=\ttfamily\small,xleftmargin=-2mm]
|
|
89 |
$ scala -cp collatz.jar
|
202
|
90 |
|
316
|
91 |
scala> CW6a.collatz(6)
|
|
92 |
res0: Long = 8
|
|
93 |
|
147
|
94 |
|
316
|
95 |
scala> import CW6a._
|
|
96 |
scala> collatz(9)
|
|
97 |
res1: Long = 19
|
|
98 |
\end{lstlisting}%$
|
202
|
99 |
|
147
|
100 |
\end{frame}
|
|
101 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
316
|
102 |
|
|
103 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
104 |
\begin{frame}[t]
|
|
105 |
\frametitle{Preliminary Part 7}
|
|
106 |
|
|
107 |
\Large
|
|
108 |
\[
|
|
109 |
\texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)}
|
|
110 |
\]\bigskip
|
|
111 |
|
|
112 |
\large
|
|
113 |
\quad{}\;where \;$d_1^2$\; means \;$d_1 \cdot d_1$\; and so on
|
|
114 |
\end{frame}
|
|
115 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
116 |
|
147
|
117 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
118 |
\begin{frame}[c]
|
317
|
119 |
\frametitle{Discussion Forum}
|
|
120 |
|
|
121 |
\large
|
318
|
122 |
``Since we can't use \code{var}s I was wondering if we could use a stack?''
|
317
|
123 |
\bigskip\bigskip\bigskip\bigskip
|
147
|
124 |
|
317
|
125 |
\small
|
|
126 |
My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each.
|
204
|
127 |
\end{frame}
|
|
128 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
129 |
|
|
130 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
131 |
\begin{frame}[t]
|
317
|
132 |
\frametitle{Email: Hate 'val'}
|
|
133 |
|
|
134 |
\mbox{}\\[-22mm]\mbox{}
|
|
135 |
|
|
136 |
\begin{center}
|
|
137 |
\begin{bubble}[10.5cm]
|
|
138 |
Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\
|
|
139 |
|
|
140 |
Hello Mr Urban,\medskip\\
|
|
141 |
|
|
142 |
I just wanted to ask, how are we suppose to work
|
|
143 |
with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is
|
|
144 |
this rule active at all? I’ve spent 4 hours not thinking on the
|
|
145 |
coursework, but how to bypass this annoying rule. What’s the whole
|
|
146 |
point of all these coursework, when we can’t use everything Scala
|
|
147 |
gives us?!?\medskip\\
|
|
148 |
|
|
149 |
Regards.\\
|
|
150 |
\mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\
|
|
151 |
\end{bubble}
|
|
152 |
\end{center}
|
|
153 |
|
|
154 |
\end{frame}
|
|
155 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
156 |
|
|
157 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
158 |
\begin{frame}[c]
|
204
|
159 |
|
317
|
160 |
\mbox{}\\[-25mm]\mbox{}
|
204
|
161 |
|
|
162 |
\begin{center}
|
|
163 |
\begin{bubble}[10.5cm]
|
317
|
164 |
Subject: \textbf{Re: Hate '\textbf{\texttt{val}}'}\hfill 01:02 AM\bigskip\bigskip\\
|
204
|
165 |
|
317
|
166 |
\textcolor{black!70}{
|
|
167 |
\textit{\large<<my usual rant about fp\ldots\\ concurrency bla bla\ldots{} better programs
|
|
168 |
yada>>}}\bigskip\bigskip\bigskip
|
|
169 |
|
|
170 |
PS: What are you trying to do where you desperately want to use \texttt{var}?
|
204
|
171 |
\end{bubble}
|
|
172 |
\end{center}
|
|
173 |
|
|
174 |
\end{frame}
|
|
175 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
176 |
|
317
|
177 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
178 |
\begin{frame}[c,fragile]
|
|
179 |
|
|
180 |
\begin{textblock}{6}(0.5,0.5)
|
|
181 |
\begin{bubble}[11.5cm]
|
|
182 |
\small
|
|
183 |
Subject: \textbf{Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:04 AM\medskip\\
|
204
|
184 |
|
317
|
185 |
\textbf{Right now my is\_legal function works fine:}
|
|
186 |
|
|
187 |
\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
|
|
188 |
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = {
|
|
189 |
var boolReturn = false
|
|
190 |
if(x._1 > dim || x._2 > dim || x._1 < 0 || x._2 < 0) {
|
|
191 |
else { var breakLoop = false
|
|
192 |
if(path == Nil) { boolReturn = true }
|
|
193 |
else { for(i <- 0 until path.length) {
|
|
194 |
if(breakLoop == false) {
|
|
195 |
if(path(i) == x) {
|
|
196 |
boolReturn = true
|
|
197 |
breakLoop = true
|
|
198 |
}
|
|
199 |
else { boolReturn = false }
|
|
200 |
} else breakLoop
|
|
201 |
}
|
|
202 |
}
|
|
203 |
boolReturn
|
|
204 |
}
|
|
205 |
\end{lstlisting}
|
|
206 |
\end{bubble}
|
|
207 |
\end{textblock}
|
204
|
208 |
|
317
|
209 |
\begin{textblock}{6}(8.2,11.8)
|
|
210 |
\begin{bubble}[5.5cm]\footnotesize\bf
|
|
211 |
\ldots{}but I can’t make it work with boolReturn being val. What approach would
|
|
212 |
you recommend in this case, and is using var in this case justified?
|
|
213 |
\end{bubble}
|
204
|
214 |
\end{textblock}
|
|
215 |
|
317
|
216 |
\only<2>{
|
|
217 |
\begin{textblock}{6}(0.3,11.8)
|
|
218 |
\begin{bubble}[3.1cm]
|
|
219 |
\textbf{Me:}
|
|
220 |
\raisebox{-12mm}{\includegraphics[scale=0.08]{../pics/throwup.jpg}}
|
|
221 |
\end{bubble}
|
|
222 |
\end{textblock}}
|
|
223 |
|
|
224 |
\end{frame}
|
|
225 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
226 |
|
|
227 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
228 |
\begin{frame}[t,fragile]
|
|
229 |
|
|
230 |
\mbox{}\\[-25mm]\mbox{}
|
|
231 |
|
|
232 |
\begin{textblock}{6}(0.5,2)
|
|
233 |
\begin{bubble}[11.5cm]
|
|
234 |
Subject: \textbf{Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:06 AM\bigskip\\
|
|
235 |
\small
|
|
236 |
|
|
237 |
OK. So you want to make sure that the \texttt{x}-position is not outside the
|
|
238 |
board....and furthermore you want to make sure that the \texttt{x}-position
|
|
239 |
is not yet in the path list. How about something like\bigskip
|
|
240 |
|
|
241 |
\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
|
|
242 |
def is_legal(dim: Int, path: Path)(x: Pos): Boolean =
|
|
243 |
...<<some board conditions>>... && !path.contains(x)
|
|
244 |
\end{lstlisting}\bigskip
|
|
245 |
|
|
246 |
\small Does not even contain a \texttt{val}.
|
|
247 |
\end{bubble}
|
|
248 |
\end{textblock}
|
|
249 |
|
|
250 |
\begin{textblock}{6}(7,12)
|
|
251 |
\footnotesize\textcolor{black!50}{(This is all on one line)}
|
204
|
252 |
\end{textblock}
|
|
253 |
|
317
|
254 |
\end{frame}
|
|
255 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
256 |
|
|
257 |
|
|
258 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
259 |
\begin{frame}[t,fragile]
|
|
260 |
|
|
261 |
\mbox{}\\[-15mm]\mbox{}
|
|
262 |
|
|
263 |
\begin{textblock}{6}(1,3)
|
|
264 |
\begin{bubble}[10.5cm]
|
|
265 |
Subject: \textbf{Re: Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 11:02 AM\bigskip\bigskip\\
|
|
266 |
|
|
267 |
THANK YOU! You made me change my coding perspective. Because of you,
|
|
268 |
I figured out the next one\ldots
|
|
269 |
\end{bubble}
|
|
270 |
\end{textblock}
|
147
|
271 |
|
317
|
272 |
\only<2>{
|
|
273 |
\begin{textblock}{6}(0.3,11.8)
|
|
274 |
\begin{bubble}[3.1cm]
|
|
275 |
\textbf{Me:}
|
|
276 |
\raisebox{-12mm}{\includegraphics[scale=0.15]{../pics/happy.jpg}}
|
|
277 |
\end{bubble}
|
|
278 |
\end{textblock}}
|
|
279 |
|
|
280 |
\end{frame}
|
|
281 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
286 |
\begin{frame}[c]
|
|
287 |
\frametitle{Assignments}
|
|
288 |
|
|
289 |
Don't change any names or types in the templates!\bigskip
|
|
290 |
|
|
291 |
Avoid at all costs:
|
|
292 |
|
|
293 |
\begin{itemize}
|
|
294 |
\item \code{var}
|
|
295 |
\item \code{return}
|
|
296 |
\item \texttt{ListBuffer}
|
|
297 |
\item \texttt{mutable}
|
|
298 |
\item \texttt{.par}
|
|
299 |
\end{itemize}\bigskip\bigskip
|
|
300 |
|
|
301 |
I cannot think of a good reason to use stacks.
|
147
|
302 |
\end{frame}
|
|
303 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
304 |
|
|
305 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
306 |
\begin{frame}[t]
|
|
307 |
\frametitle{For-Comprehensions Again}
|
|
308 |
|
|
309 |
\begin{center}
|
|
310 |
\begin{tikzpicture}[scale=1,
|
|
311 |
node/.style={
|
|
312 |
rectangle,rounded corners=3mm,
|
|
313 |
very thick,draw=black!50,
|
|
314 |
minimum height=18mm, minimum width=20mm,
|
|
315 |
top color=white,bottom color=black!20}]
|
|
316 |
|
|
317 |
\node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
|
|
318 |
\node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
|
|
319 |
\node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
|
|
320 |
\node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
|
|
321 |
\node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
|
|
322 |
\node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
|
|
323 |
\node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
|
|
324 |
|
|
325 |
\onslide<2->{
|
|
326 |
\node (B0) at (1.4,-3) {\texttt{List(}};
|
|
327 |
\node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
|
|
328 |
\node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
|
|
329 |
\node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
|
|
330 |
\node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
|
|
331 |
\node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
|
|
332 |
|
|
333 |
\onslide<2->{
|
|
334 |
\draw [->,line width=1mm] (A1.south) -- (B1.north);
|
|
335 |
\draw [->,line width=1mm] (A2.south) -- (B2.north);
|
|
336 |
\draw [->,line width=1mm] (A3.south) -- (B3.north);
|
|
337 |
\draw [->,line width=1mm] (A4.south) -- (B4.north);
|
|
338 |
\draw [->,line width=1mm] (A5.south) -- (B5.north);}
|
|
339 |
|
|
340 |
\onslide<2->{
|
|
341 |
\node (Q1) at (-0.45,-0.1) {};
|
|
342 |
\node (Q2) at (-0.45,-2.8) {};
|
|
343 |
\node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
|
|
344 |
\draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
|
|
345 |
\end{tikzpicture}
|
|
346 |
\end{center}
|
|
347 |
|
|
348 |
\onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
|
|
349 |
|
|
350 |
\end{frame}
|
|
351 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
352 |
|
|
353 |
|
|
354 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
355 |
\begin{frame}[t]
|
|
356 |
\frametitle{For-Comprehensions Again}
|
|
357 |
|
|
358 |
\begin{center}
|
|
359 |
\begin{tikzpicture}[scale=1,
|
|
360 |
node/.style={
|
|
361 |
rectangle,rounded corners=3mm,
|
|
362 |
very thick,draw=black!50,
|
|
363 |
minimum height=18mm, minimum width=20mm,
|
|
364 |
top color=white,bottom color=black!20}]
|
|
365 |
|
|
366 |
\node (A0) at (0,0)
|
|
367 |
{\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5))
|
|
368 |
\textcolor{purple}{\textbf{yield}} n\,*\,n}};
|
|
369 |
|
|
370 |
\node (A1) at (0,-1.5) {\LARGE\textbf{vs}};
|
|
371 |
|
|
372 |
\node (A2) at (0,-3)
|
|
373 |
{\texttt{\textcolor{purple}{\textbf{for}} (n <- List(1, 2, 3, 4, 5)) println(n)}};
|
|
374 |
\end{tikzpicture}
|
|
375 |
\end{center}\bigskip
|
|
376 |
|
|
377 |
|
|
378 |
The second version is in case the for \textbf{does not}
|
|
379 |
produce any result.
|
|
380 |
|
|
381 |
\end{frame}
|
|
382 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
383 |
|
|
384 |
|
|
385 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
318
|
386 |
\begin{frame}[c,fragile]
|
|
387 |
%\frametitle{Option Type}
|
|
388 |
|
|
389 |
Find something below 4 in a list. What do you think Scala answers?\bigskip\bigskip
|
|
390 |
|
|
391 |
\begin{onlyenv}<1>
|
|
392 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
|
|
393 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
394 |
|
|
395 |
List(5,6,7,8,9).find(_ < 4)
|
|
396 |
\end{lstlisting}
|
|
397 |
\end{onlyenv}
|
|
398 |
\begin{onlyenv}<2>
|
|
399 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
|
|
400 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
401 |
res: Option[Int] = Some(2)
|
|
402 |
|
|
403 |
List(5,6,7,8,9).find(_ < 4)
|
|
404 |
res: Option[Int] = None
|
|
405 |
\end{lstlisting}
|
|
406 |
\end{onlyenv}
|
|
407 |
|
|
408 |
\end{frame}
|
|
409 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
410 |
|
|
411 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
412 |
\begin{frame}[c]
|
|
413 |
\frametitle{Option Type}
|
|
414 |
|
|
415 |
\begin{itemize}
|
|
416 |
\item if the value is present, you use\bigskip
|
|
417 |
\begin{center}\pcode{Some(value)}\end{center}\bigskip\bigskip
|
|
418 |
|
|
419 |
\item if no value is present, you use\bigskip
|
|
420 |
\begin{center}\pcode{None}\end{center}\bigskip\bigskip
|
|
421 |
\end{itemize}
|
|
422 |
|
|
423 |
\small e.g.~\code{Option[Int]}, then \code{Some(42)} and \code{None}\\
|
|
424 |
good for error handling
|
|
425 |
\end{frame}
|
|
426 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
427 |
|
|
428 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
429 |
\begin{frame}[c,fragile]
|
317
|
430 |
\frametitle{Option Type}
|
66
|
431 |
|
318
|
432 |
\small
|
|
433 |
\begin{onlyenv}<1>
|
|
434 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
|
|
435 |
Integer.parseInt("1234")
|
66
|
436 |
|
318
|
437 |
// vs.
|
|
438 |
|
|
439 |
def get_me_an_int(s: String) : Option[Int] =
|
|
440 |
Try(Some(Integer.parseInt(s))).getOrElse(None)
|
|
441 |
\end{lstlisting}
|
|
442 |
\end{onlyenv}\bigskip\bigskip\bigskip
|
|
443 |
|
|
444 |
in the Scala code it is clear from the type I have to deal
|
|
445 |
with the \pcode{None}-case; no JavaDoc needed
|
66
|
446 |
|
|
447 |
\end{frame}
|
147
|
448 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
318
|
449 |
|
66
|
450 |
|
317
|
451 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
318
|
452 |
\begin{frame}[c,fragile]
|
317
|
453 |
\frametitle{Higher-Order Functions}
|
|
454 |
|
318
|
455 |
In Scala, functions can take other functions as arguments and can return
|
|
456 |
a function as a result.\bigskip\bigskip
|
|
457 |
|
|
458 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
|
|
459 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
460 |
\end{lstlisting}
|
|
461 |
|
|
462 |
\UParrow{1}{10}{11}
|
|
463 |
\end{frame}
|
|
464 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
317
|
465 |
|
318
|
466 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
467 |
\begin{frame}[c,fragile]
|
|
468 |
\frametitle{Higher-Order Functions (2)}
|
|
469 |
|
|
470 |
|
|
471 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
|
|
472 |
def even(x: Int) : Boolean = x % 2 == 0
|
|
473 |
|
|
474 |
List(1, 2, 3, 4, 5).filter(even)
|
|
475 |
res : List[Int] = List(2, 4)
|
|
476 |
|
|
477 |
List(1, 2, 3, 4, 5).count(even)
|
|
478 |
res : Int = 2
|
|
479 |
|
|
480 |
List(1, 2, 3, 4, 5).find(even)
|
|
481 |
res: Option[Int] = Some(2)
|
|
482 |
\end{lstlisting}
|
317
|
483 |
|
|
484 |
\end{frame}
|
|
485 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
318
|
486 |
|
|
487 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
488 |
\begin{frame}[c,fragile]
|
|
489 |
\frametitle{map (lower case)}
|
|
490 |
|
|
491 |
applies a function to each element of a list (and more)
|
|
492 |
|
|
493 |
\begin{center}
|
|
494 |
\begin{tikzpicture}[scale=0.9]
|
|
495 |
|
|
496 |
\node (A0) at (1.2,0) {\texttt{List(\,}};
|
|
497 |
\node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
|
|
498 |
\node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
|
|
499 |
\node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
|
|
500 |
\node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
|
|
501 |
\node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
|
|
502 |
\node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
|
|
503 |
\node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
|
|
504 |
\node (A8) at (8.3,0) {\texttt{8)}};
|
|
505 |
|
|
506 |
\node (B0) at (1.2,-3) {\texttt{List(\,}};
|
|
507 |
\node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
|
|
508 |
\node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
|
|
509 |
\node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
|
|
510 |
\node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
|
|
511 |
\node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
|
|
512 |
\node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
|
|
513 |
\node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
|
|
514 |
\node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
|
|
515 |
|
|
516 |
\draw [->,line width=1mm] (A1.south) -- (B1.north);
|
|
517 |
\draw [->,line width=1mm] (A2.south) -- (B2.north);
|
|
518 |
\draw [->,line width=1mm] (A3.south) -- (B3.north);
|
|
519 |
\draw [->,line width=1mm] (A4.south) -- (B4.north);
|
|
520 |
\draw [->,line width=1mm] (A5.south) -- (B5.north);
|
|
521 |
\draw [->,line width=1mm] (A6.south) -- (B6.north);
|
|
522 |
\draw [->,line width=1mm] (A7.south) -- (B7.north);
|
|
523 |
\draw [->,line width=1mm] (A8.south) -- (B8.north);
|
|
524 |
|
|
525 |
\node [red] (Q0) at (-0.5,-0.3) {\large\texttt{n}};
|
|
526 |
\node (Q1) at (-0.5,-0.4) {};
|
|
527 |
\node (Q2) at (-0.5,-2.5) {};
|
|
528 |
\node [red] (Q3) at (-0.5,-2.65) {\large\texttt{n\,*\,n}};
|
|
529 |
\draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);
|
|
530 |
|
|
531 |
\node [red] at (-1.5,-1.5) {\Large{}\it\textbf{map}};
|
|
532 |
\end{tikzpicture}
|
|
533 |
\end{center}\bigskip
|
|
534 |
|
|
535 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
|
|
536 |
List(1,2,3,4,5,6,7,8).map(n => n * n)
|
|
537 |
\end{lstlisting}
|
|
538 |
\end{frame}
|
|
539 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
317
|
540 |
|
318
|
541 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
542 |
\begin{frame}[c,fragile]
|
|
543 |
\frametitle{For-Comprehensions are maps}
|
|
544 |
|
|
545 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
|
|
546 |
for (n <- List(1,2,3,4,5,6,7,8))
|
|
547 |
yield n * n
|
|
548 |
|
|
549 |
|
|
550 |
// is just syntactic sugar for
|
|
551 |
|
|
552 |
|
|
553 |
List(1,2,3,4,5,6,7,8).map(n => n * n)
|
|
554 |
\end{lstlisting}
|
|
555 |
|
|
556 |
\end{frame}
|
|
557 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
558 |
|
|
559 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
560 |
\begin{frame}[c,fragile]
|
|
561 |
\frametitle{Map (upper case)}
|
|
562 |
|
|
563 |
a type, representing a key-value association datastructure\bigskip\bigskip
|
|
564 |
|
|
565 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
|
|
566 |
val ascii =
|
|
567 |
('a' to 'z').map(c => (c, c.toInt))
|
|
568 |
|
|
569 |
val ascii_Map = ascii.toMap
|
|
570 |
|
|
571 |
ascii_Map.get('a') // -> 97
|
|
572 |
\end{lstlisting}
|
|
573 |
\end{frame}
|
|
574 |
|
|
575 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
576 |
\begin{frame}[c,fragile]
|
|
577 |
\frametitle{Recursion}
|
|
578 |
|
|
579 |
|
|
580 |
|
|
581 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm]
|
|
582 |
def fib(n: Int) : Int = {
|
|
583 |
if (n == 0 || n == 1) 1
|
|
584 |
else fib(n - 1) + fib(n - 2)
|
|
585 |
}
|
|
586 |
\end{lstlisting}
|
|
587 |
\end{frame}
|
|
588 |
|
|
589 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
590 |
\begin{frame}[c,fragile]
|
|
591 |
\frametitle{Recursion}
|
|
592 |
|
|
593 |
\small
|
|
594 |
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-4mm]
|
|
595 |
def my_flatten(xs: List[Option[Int]]): List[Int] =
|
|
596 |
xs match {
|
|
597 |
case Nil => Nil
|
|
598 |
case None :: rest => my_flatten(rest)
|
|
599 |
case Some(v) :: rest => v :: my_flatten(rest)
|
|
600 |
}
|
|
601 |
\end{lstlisting}
|
|
602 |
\end{frame}
|
|
603 |
|
|
604 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
317
|
605 |
|
148
|
606 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
316
|
607 |
\begin{frame}[c]
|
|
608 |
\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
|
66
|
609 |
|
148
|
610 |
\begin{center}
|
316
|
611 |
\begin{tabular}[t]{@{}l@{}l@{}}
|
|
612 |
\includegraphics[scale=0.1]{../pics/mand4.png} & \hspace{4mm}
|
|
613 |
\raisebox{0mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}
|
|
614 |
\end{tabular}
|
|
615 |
\end{center}
|
|
616 |
|
|
617 |
\begin{center}
|
|
618 |
My Office Hours: Thursdays 12 -- 14\\
|
|
619 |
And specifically for Scala: Tuesdays 10:45 -- 11:45
|
148
|
620 |
\end{center}
|
66
|
621 |
\end{frame}
|
|
622 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
623 |
|
|
624 |
|
318
|
625 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
626 |
\begin{frame}[t]
|
|
627 |
|
|
628 |
\begin{center}
|
|
629 |
\includegraphics[scale=0.3]{../pics/blow.png}
|
|
630 |
\end{center}
|
|
631 |
|
|
632 |
\begin{textblock}{14}(2,11.4)
|
|
633 |
\large\bf{}Mind-Blowing Programming Languages:\\
|
|
634 |
Overloading in any language is great but it makes a difference\; \code{10/3}
|
|
635 |
\;or\; \code{10.0/3}
|
|
636 |
\end{textblock}
|
|
637 |
\end{frame}
|
|
638 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
639 |
|
|
640 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
641 |
\begin{frame}[t]
|
|
642 |
|
|
643 |
\begin{center}
|
|
644 |
\includegraphics[scale=0.3]{../pics/blow.png}
|
|
645 |
\end{center}
|
|
646 |
|
|
647 |
\begin{textblock}{14}(2,11.4)
|
|
648 |
\large\bf{}Mind-Blowing Programming Languages:\\
|
|
649 |
\centering PHP
|
|
650 |
\end{textblock}
|
|
651 |
\end{frame}
|
|
652 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
653 |
|
|
654 |
|
66
|
655 |
\end{document}
|
|
656 |
|
|
657 |
%%% Local Variables:
|
|
658 |
%%% mode: latex
|
|
659 |
%%% TeX-master: t
|
|
660 |
%%% End:
|
|
661 |
|