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