slides/slides03.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 15:58:36 +0000
changeset 378 7a5ad01a85b5
parent 367 e6ae724255a8
child 382 1bd800376e0c
permissions -rw-r--r--
updated

% !TEX program = xelatex
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
\usepackage{../slides}
\usepackage{../graphics}
\usepackage{../langs}
%%\usepackage{../data}
\usetikzlibrary{shapes}
\usepackage[export]{adjustbox}

\hfuzz=220pt 

%\setmonofont[Scale=.88]{Consolas}
%\newfontfamily{\consolas}{Consolas}

\lstset{language=Scala,
        style=mystyle,
        numbersep=0pt,
        numbers=none,
        xleftmargin=0mm}

\newcommand{\bl}[1]{\textcolor{blue}{#1}}     

% beamer stuff 
\renewcommand{\slidecaption}{PEP (Scala) 03, King's College London}

\newcommand{\UParrow}[3]{%
\begin{textblock}{0}(#2,#3)%
\onslide<#1>{%
\begin{tikzpicture}%
\node at (0,0) [single arrow, shape border rotate=90, fill=red,text=red]{a};%
\end{tikzpicture}}%
\end{textblock}}

\newcommand{\DOWNarrow}[3]{%
\begin{textblock}{0}(#2,#3)%
\onslide<#1>{%
\begin{tikzpicture}%
\node at (0,0) [single arrow, shape border rotate=270, fill=red,text=red]{a};%
\end{tikzpicture}}%
\end{textblock}}

\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
\frametitle{%
  \begin{tabular}{@ {}c@ {}}
  \\[5mm]
  \huge PEP Scala (3) 
  \end{tabular}}

  \normalsize
  \begin{center}
  \begin{tabular}{ll}
    Email:  & christian.urban at kcl.ac.uk\\
    %Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
    Slides \& Code: & KEATS\bigskip\\
    %Office Hours: &  Thursdays 12:00 -- 14:00\\
    %Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
  \end{tabular}
  \end{center}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
% \begin{frame}[c]
% \frametitle{Preliminary 6}

% Raw marks (261 submissions):\bigskip

% \begin{itemize}
% \item 3\%: \hspace{4mm}219
% \item 2\%: \hspace{4mm}19
% \item 1\%: \hspace{4mm}0
% \item 0\%: \hspace{4mm}23 \;(4 no submission)
% \end{itemize}  
% \end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]

\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
def collatz(n: Long) : Long =
  {
    val toReturn = collatzHelper(n, 0)
    toReturn
  } 
\end{lstlisting}

\pause
\bigskip
\rule{11cm}{0.3mm}
\bigskip

\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
def collatz(n: Long) : Long =
  collatzHelper(n, 0)
\end{lstlisting}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c,fragile]
\frametitle{Default Arguments}

\small
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
def collatzHelper(n: Int, a: Int = 0) : Int = ...


collatzHelper(n, 3)
collatzHelper(n, 0)

collatzHelper(n)   // a = 0   
\end{lstlisting}

\DOWNarrow{1}{10.7}{3.4}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Last Week: Options \& HO Funs.}

\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
List(7,2,3,4,5,6).find(_ < 4)
res: Option[Int] = Some(2)
 

List(5,6,7,8,9).find(_ < 4)
res: Option[Int] = None


List(1,2,3,4,5).map(x => x * x)
res: List[Int] = List(1, 4, 9, 16, 25)
\end{lstlisting}
  
  \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Web-Crawler (1)}

\small
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-7mm]
def get_page(url: String) : String = {
Try(fromURL(url)("ISO-8859-1").take(10000).mkString)
   .getOrElse { println(s" Problem with: $url"); ""}
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Web-Crawler (2)}

\small
\begin{lstlisting}[language=Scala, numbers=none, 
                    xleftmargin=-7mm, escapeinside={(*@}{@*)}]
val http_pattern = """(*@\textcolor{codegreen}{"}@*)https?://[\^(*@\textcolor{codegreen}{"}@*)]*(*@\textcolor{codegreen}{"}@*)""".r
val email_pattern = 
 """([a-z\d\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r


def unquote(s: String) = s.drop(1).dropRight(1)


def get_all_URLs(page: String): Set[String] = 
  http_pattern.findAllIn(page).map(unquote).toSet

  // returns all URLs in a page  
\end{lstlisting}
  
  \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Web-Crawler (3)}

\small
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
def crawl(url: String, n: Int) : Unit = {
  if (n == 0) ()
  else {
    println(s"  Visiting: $n $url")
    val page = get_page(url)
    for (u <- get_all_URLs(page)) 
      crawl(u, n - 1)
  }
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Email Harvester}

\small
\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
def emails(url: String, n: Int) : Set[String] = {
 if (n == 0) Set()
 else {
  println(s"  Visiting: $n $url")
  val page = get_page(url)
  val new_emails = 
    email_pattern.findAllIn(page).toSet
  new_emails ++ 
    (for (u <- get_all_URLs(page)) 
       yield emails(u, n - 1)).flatten
 }
} 

\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c]
\frametitle{Jumping Towers}

\begin{center}
\begin{tikzpicture}[scale=1.3]
  \draw[line width=1mm,cap=round] (0,0) -- (5,0);
  \draw[line width=1mm,cap=round] (0,1) -- (5,1);

  \draw[line width=1mm,cap=round] (0,0) -- (0,1);
  \node at (0.5,0.5) {\textbf{\Large 3}};

  \draw[line width=1mm,cap=round] (1,0) -- (1,1);
  \node at (1.5,0.5) {\textbf{\Large 4}};

  \draw[line width=1mm,cap=round] (2,0) -- (2,1);
  \node at (2.5,0.5) {\textbf{\Large 2}};

  \draw[line width=1mm,cap=round] (3,0) -- (3,1);
  \node at (3.5,0.5) {\textbf{\Large 0}};
  
  \draw[line width=1mm,cap=round] (4,0) -- (4,1);

  \node at (4.5,0.5) {\textbf{\Large 1}};
  
  \draw[line width=1mm,cap=round] (5,0) -- (5,1);

  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (1.5,1);
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (2.5,1);
  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (0.5,1) to (3.5,1);

  \draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (3.5,0);
  \draw[->,line width=0.5mm,cap=round,out=-90,in=-90,relative] (2.5,0) to (4.5,0);

  \draw[->,line width=0.5mm,cap=round,out=90,in=90,relative] (4.5,1) to (5.7,1);
  \node at (5.7, 0.8) {End};
\end{tikzpicture}
\end{center}\bigskip


shortest: 3 $\rightarrow$ 4 $\rightarrow$ End

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c]
\frametitle{next moves}

\begin{center}
  \begin{tikzpicture}
    [grow=right,level distance=30mm,child anchor=north,line width=0.5mm]
  \node {$[3,4,2,0,1]$}
     child {node {$[0,1]$}}
     child {node {$[2,0,1]$}
        child {node {$[1]$} child [level distance=13mm] {node {End}}}
        child {node {$[0,1]$}}
     }
     child {node {$[4,2,0,1]$\ldots}};
\end{tikzpicture}
\end{center}

\begin{textblock}{4}(13,12)
\includegraphics[scale=0.06]{../pics/chess.jpg}
\end{textblock}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
\begin{frame}[c,fragile]
\frametitle{Reverse Polish Notation}

{\Large\bl{$(3 + 1) * (2 + 9)$}}\bigskip

{\Large$\Rightarrow$}\bigskip

{\;\;\Large\bl{$3\;\;1\;+\;2\;\;9\;+\;*$}}

\begin{textblock}{3}(11,4)
\begin{onlyenv}<2>
\begin{lstlisting}[language=JVMIS]
ldc 3
ldc 1
iadd
ldc 2
ldc 9
iadd
imul
\end{lstlisting}
\end{onlyenv} 
\end{textblock}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
% \begin{frame}[c,fragile]
% \frametitle{Sudoku}

% A very simple-minded version on 110 problems:\bigskip

% \begin{itemize}
% \item 1 core: 800 secs
% \item 2 cores: 400 secs
% \item 8 cores: 290 secs
% \item 18 cores: 142 secs
% \end{itemize}

% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[t]

%   \begin{center}  
%   \includegraphics[scale=0.3]{../pics/blow.png}
%   \end{center}
  
%   \begin{textblock}{14}(2,11.4)
%   \large\bf{}Mind-Blowing Programming Languages:\\ 
%   \centering JavaScript
%   \end{textblock}
% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}<1-10>
\end{frame}

\end{document}


\end{document}

%%% Local Variables:  
%%% mode: latex
%%% TeX-master: t
%%% End: