slides/slides02.tex
changeset 318 029e2862bb4e
parent 317 607ceabeeffc
child 319 b84ea52bfd8f
equal deleted inserted replaced
317:607ceabeeffc 318:029e2862bb4e
     1 % !TEX program = xelatex
     1 % !TEX program = xelatex
     2 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
     2 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
     3 \usepackage{chessboard}
     3 %\usepackage{chessboard}
     4 \usepackage[LSBC4,T1]{fontenc}
     4 %\usepackage[LSBC4,T1]{fontenc}
     5 \usepackage{../slides}
     5 \usepackage{../slides}
     6 \usepackage{../graphics}
     6 \usepackage{../graphics}
     7 \usepackage{../langs}
     7 \usepackage{../langs}
     8 
     8 \usetikzlibrary{shapes}
     9 % \usepackage{../data}
     9 % \usepackage{../data}
    10 
    10 
    11 \hfuzz=220pt 
    11 \hfuzz=220pt 
    12 
    12 
    13 %\setmonofont[Scale=.88]{Consolas}
    13 %\setmonofont[Scale=.88]{Consolas}
    22 \newcommand{\bl}[1]{\textcolor{blue}{#1}}     
    22 \newcommand{\bl}[1]{\textcolor{blue}{#1}}     
    23 
    23 
    24 % beamer stuff 
    24 % beamer stuff 
    25 \renewcommand{\slidecaption}{PEP (Scala) 02, King's College London}
    25 \renewcommand{\slidecaption}{PEP (Scala) 02, King's College London}
    26 
    26 
       
    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}}
    27 
    34 
    28 \begin{document}
    35 \begin{document}
    29 
    36 
    30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    31 \begin{frame}[t]
    38 \begin{frame}[t]
    50 \end{frame}
    57 \end{frame}
    51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    52 
    59 
    53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    54 \begin{frame}[c,fragile]
    61 \begin{frame}[c,fragile]
    55   \frametitle{My Scala Version}
    62   \frametitle{Scala 2.13.1}
    56   
    63   
    57   \begin{lstlisting}[language={},numbers=none,
    64   \begin{lstlisting}[language={},numbers=none,
    58     basicstyle=\ttfamily\small,xleftmargin=-2mm]
    65     basicstyle=\ttfamily\small,xleftmargin=-2mm]
    59   $ scala
    66   $ scala
    60     
    67     
   110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   111 \begin{frame}[c]
   118 \begin{frame}[c]
   112   \frametitle{Discussion Forum}
   119   \frametitle{Discussion Forum}
   113   
   120   
   114   \large
   121   \large
   115   ``Since we cant use \code{var}s I was wondering if we could use a stack?''
   122   ``Since we can't use \code{var}s I was wondering if we could use a stack?''
   116   \bigskip\bigskip\bigskip\bigskip
   123   \bigskip\bigskip\bigskip\bigskip
   117 
   124 
   118   \small
   125   \small
   119   My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each. 
   126   My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each. 
   120 \end{frame}
   127 \end{frame}
   373 
   380 
   374 \end{frame}
   381 \end{frame}
   375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   376 
   383 
   377 
   384 
   378 
   385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   386 \begin{frame}[c,fragile]
   380 \begin{frame}[t]
   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]
   381 \frametitle{Option Type}
   413 \frametitle{Option Type}
   382 
   414     
   383 
   415 \begin{itemize}
   384   
   416 \item if the value is present, you use\bigskip
   385 \end{frame}
   417 \begin{center}\pcode{Some(value)}\end{center}\bigskip\bigskip
   386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   418 
   387 
   419 \item if no value is present, you use\bigskip
   388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   420 \begin{center}\pcode{None}\end{center}\bigskip\bigskip
   389 \begin{frame}[t]
   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]
       
   430 \frametitle{Option Type}
       
   431 
       
   432 \small   
       
   433 \begin{onlyenv}<1>
       
   434 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
       
   435 Integer.parseInt("1234")
       
   436 
       
   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
       
   446   
       
   447 \end{frame}
       
   448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   449  
       
   450 
       
   451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   452 \begin{frame}[c,fragile]
   390 \frametitle{Higher-Order Functions}
   453 \frametitle{Higher-Order Functions}
   391   
   454   
   392   
   455 In Scala, functions can take other functions as arguments and can return 
   393     
   456 a function as a result.\bigskip\bigskip  
   394 \end{frame}
   457 
   395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   458 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm]
   396   
   459 List(7,2,3,4,5,6).find(_ < 4)
       
   460 \end{lstlisting}
       
   461     
       
   462 \UParrow{1}{10}{11}    
       
   463 \end{frame}
       
   464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   465   
       
   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}
       
   483     
       
   484 \end{frame}
       
   485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   540   
       
   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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   397 
   605 
   398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   399 \begin{frame}[c]
   607 \begin{frame}[c]
   400 \frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
   608 \frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
   401 
   609 
   412 \end{center}
   620 \end{center}
   413 \end{frame}
   621 \end{frame}
   414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   415 
   623 
   416 
   624 
       
   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 
   417 \end{document}
   655 \end{document}
   418 
   656 
   419 %%% Local Variables:  
   657 %%% Local Variables:  
   420 %%% mode: latex
   658 %%% mode: latex
   421 %%% TeX-master: t
   659 %%% TeX-master: t