# HG changeset patch # User Christian Urban # Date 1478750565 0 # Node ID 45557ad18ea677a2a9ca1326072192316b48b36c # Parent d0caa12ab8d8dbfb655a851cfb4564e8aee6e133 updated diff -r d0caa12ab8d8 -r 45557ad18ea6 cws/cw01.pdf Binary file cws/cw01.pdf has changed diff -r d0caa12ab8d8 -r 45557ad18ea6 graphics.sty --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graphics.sty Thu Nov 10 04:02:45 2016 +0000 @@ -0,0 +1,21 @@ +\usepackage{tikz} +\usepackage{pgf} +\usetikzlibrary{positioning} +\usetikzlibrary{calc} +\usetikzlibrary{automata} +\usetikzlibrary{arrows} +\usetikzlibrary{backgrounds} +\usetikzlibrary{fit} +\usepackage{tikz-qtree} +\usepackage{graphicx} +\usepackage{pgfplots} + +\pgfplotsset{compat=1.11} + +\newenvironment{bubble}[1][]{% +\addtolength{\leftmargini}{4mm}% +\begin{tikzpicture}[baseline=(current bounding box.north)]% +\draw (0,0) node[inner sep=2mm,fill=cream,ultra thick,draw=red,rounded corners=2mm]% +\bgroup\begin{minipage}{#1}\raggedright{}} +{\end{minipage}\egroup;% +\end{tikzpicture}\bigskip} diff -r d0caa12ab8d8 -r 45557ad18ea6 pics/laptop.png Binary file pics/laptop.png has changed diff -r d0caa12ab8d8 -r 45557ad18ea6 pics/servers.png Binary file pics/servers.png has changed diff -r d0caa12ab8d8 -r 45557ad18ea6 progs/hello-world.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/hello-world.scala Thu Nov 10 04:02:45 2016 +0000 @@ -0,0 +1,3 @@ +object Hello extends App { + println("hello world") +} diff -r d0caa12ab8d8 -r 45557ad18ea6 progs/lecture1.scala --- a/progs/lecture1.scala Thu Nov 10 01:31:12 2016 +0000 +++ b/progs/lecture1.scala Thu Nov 10 04:02:45 2016 +0000 @@ -43,6 +43,7 @@ // some methods take more than one argument println(lst.mkString("[", ",", "]")) + // Conversion methods //==================== @@ -52,7 +53,19 @@ 1.toDouble +// useful list methods + +List(1,2,3,4).length List(1,2,3,4).reverse +List(1,2,3,4).max +List(1,2,3,4).min +List(1,2,3,4).sum +List(1,2,3,4).take(2).sum +List(1,2,3,4).drop(2).sum +List(1,2,3,4,3).indexOf(3) + +"1,2,3,4,5".split(",").toList + // Types //======= @@ -107,10 +120,20 @@ // Hello World //============= -// show an example of a stand-alone scala file -// remind that in the course work you are asked a -// plain scala "work-sheet" +// an example of a stand-alone scala file; +// in the coursework students must submit +// plain scala "work-sheets" +object Hello extends App { + println("hello world") +} + +// can be called with +// +// $> scalac hello-world.scala +// $> scala Hello +// +// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello // Function Definitions @@ -175,6 +198,11 @@ // Assert/Testing //================ +assert(gcd(48, 18) == 6) + +assert(gcd(48, 18) == 5, "The gcd test failed") + + // For-Comprehensions (not For-Loops) //==================================== @@ -191,6 +219,14 @@ mult_table.sliding(10,10).mkString("\n") +// with if-predicates + +for (n <- (1 to 3).toList; + m <- (1 to 3).toList; + if (n + m) % 2 == 0) yield (n, m) + + + // with patterns for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n @@ -199,5 +235,133 @@ +// with only a side-effect (no list is produced) +// has no "yield" + +for (n <- (1 to 10)) println(n) + + +// concurrency (ONLY WORKS IN 2.11.8) +for (n <- (1 to 10)) println(n) +for (n <- (1 to 10).par) println(n) + + + +// for testing time +def time_needed[T](i: Int, code: => T) = { + val start = System.nanoTime() + for (j <- 1 to i) code + val end = System.nanoTime() + ((end - start) / i / 1.0e9) + " secs" +} + +val list = (1 to 1000000).toList +time_needed(10, for (n <- list) yield n + 42) +time_needed(10, for (n <- list.par) yield n + 42) + + + // Webpages //========== + +import io.Source + +val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" +Source.fromURL(url)("ISO-8859-1").mkString + + + +def price_lookup(symbol: String): String = { + val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1" + Source.fromURL(url).mkString.drop(1).dropRight(2) +} + +price_lookup("GOOG") +price_lookup("AAPL") + + +val companies = + List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") + +for (s <- companies.par) println(price_lookup(s)) + + +// A Web Crawler +//=============== + +import io.Source +import scala.util.matching.Regex +import scala.util._ + +// gets the first 10K of a web-page +def get_page(url: String) : String = { + Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString). + getOrElse { println(s" Problem with: $url"); ""} +} + +// regex for URLs +val http_pattern = """"https?://[^"]*"""".r + +// drops the first and last character from a string +def unquote(s: String) = s.drop(1).dropRight(1) + +def get_all_URLs(page: String): Set[String] = + http_pattern.findAllIn(page).map(unquote).toSet + +// naive version of crawl - searches until a given depth, +// visits pages potentially more than once +def crawl(url: String, n: Int): Unit = { + if (n == 0) () + else { + println(s"Visiting: $n $url") + for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1) + } +} + +// some starting URLs for the crawler +val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc""" +//val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney""" + +crawl(startURL, 2) + + + + +// Adding your own methods to Strings +//==================================== + +// imagine you want to implement an additional +// method to strings, like +// +// "HAL".increment +// +// you can avoid ugly fudges, like a MyString +// class by using implicit conversions + + +implicit class MyString(s: String) { + def increment = for (c <- s) yield (c + 1).toChar +} + +"HAL".increment + + +// Further Information +//===================== + +// Scala download +// +// http://www.scala-lang.org + +// Eclipse for Scala +// +// http://scala-ide.org/download/sdk.html + + +// library docs +// +// http://www.scala-lang.org/api/current/ + +// tutorials +// +// http://docs.scala-lang.org/tutorials/ diff -r d0caa12ab8d8 -r 45557ad18ea6 slides/Point.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slides/Point.java Thu Nov 10 04:02:45 2016 +0000 @@ -0,0 +1,12 @@ +public class Point { + private final int x, y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int x() { return x; } + + public int y() { return y; } +} diff -r d0caa12ab8d8 -r 45557ad18ea6 slides/Point.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slides/Point.scala Thu Nov 10 04:02:45 2016 +0000 @@ -0,0 +1,1 @@ +class Point(val x: Int, val y: Int) diff -r d0caa12ab8d8 -r 45557ad18ea6 slides/URLReader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slides/URLReader.java Thu Nov 10 04:02:45 2016 +0000 @@ -0,0 +1,32 @@ +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Scanner; + +public class URLReader { + + public static String readURL(String sUrl) { + StringBuilder buf = new StringBuilder(); + Scanner in = null; + + try { + URL url = new URL(sUrl); + in = new Scanner(url.openStream()); + + while (in.hasNextLine()) { + buf.append(in.nextLine() + "\n"); + } + return buf.toString(); + + } catch (MalformedURLException e) { + System.err.println(e); + } catch (IOException e) { + System.err.println(e); + } finally { + if (in != null) { + in.close(); + } + } + return null; + } +} \ No newline at end of file diff -r d0caa12ab8d8 -r 45557ad18ea6 slides/slides01.pdf Binary file slides/slides01.pdf has changed diff -r d0caa12ab8d8 -r 45557ad18ea6 slides/slides01.tex --- a/slides/slides01.tex Thu Nov 10 01:31:12 2016 +0000 +++ b/slides/slides01.tex Thu Nov 10 04:02:45 2016 +0000 @@ -1,6 +1,6 @@ \documentclass[dvipsnames,14pt,t,xelatex]{beamer} \usepackage{../slides} -%\usepackage{../graphics} +\usepackage{../graphics} \usepackage{../langs} %\usepackage{../data} @@ -79,7 +79,10 @@ \frametitle{Why Scala?} \begin{itemize} -\item bla +\item compiles to the JVM (also JavaScript, X86)\medskip +\item integrates seamlessly with Java\medskip +\item combines {\bf functional} and {\bf object-oriented} programming\medskip +\item allows often to write more elegant code \end{itemize} \end{frame} @@ -87,6 +90,29 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] +\frametitle{Java vs Scala} + +{\lstset{language=java}\fontsize{12}{12}\selectfont +\texttt{\lstinputlisting{Point.java}}} + +{\lstset{language=scala}\fontsize{12}{12}\selectfont +\texttt{\lstinputlisting{Point.scala}}} + +\begin{textblock}{6}(13,3) +\textbf{\large Java} +\end{textblock} + +\begin{textblock}{6}(13,13.2) +\textbf{\large Scala} +\end{textblock} + +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] \frametitle{Types} \begin{itemize} @@ -124,6 +150,62 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] +\frametitle{An Http Request} + +\begin{textblock}{1}(2,5) +\begin{tabular}{c} +\includegraphics[scale=0.15]{../pics/servers.png}\\[-2mm] +\small Server +\end{tabular} +\end{textblock} + +\begin{textblock}{1}(5.6,4) + \begin{tikzpicture}[scale=1.1] + \draw[white] (0,1) node (X) {}; + \draw[white] (2,1) node (Y) {}; + \draw[white] (0,0) node (X1) {}; + \draw[white] (2,0) node (Y1) {}; + \draw[white] (0,-1) node (X2) {}; + \draw[white] (2,-1) node (Y2) {}; + \draw[red, <-, line width = 2mm] (X) -- (Y); + \node [inner sep=5pt,label=above:\textcolor{black}{GET request}] at ($ (X)!.5!(Y) $) {}; + \draw[red, ->, line width = 2mm] (X1) -- (Y1); + \node [inner sep=5pt,label=above:\textcolor{black}{webpage}] at ($ (X1)!.5!(Y1) $) {}; + \draw[red, <-, line width = 2mm] (X2) -- (Y2); + \node [inner sep=7pt,label=above:\textcolor{black}{POST data}] at ($ (X2)!.5!(Y2) $) {}; + \end{tikzpicture} +\end{textblock} + + +\begin{textblock}{1}(9,5.5) +\begin{tabular}{c} +\includegraphics[scale=0.15]{../pics/laptop.png}\\[-2mm] +\small Browser +\end{tabular} +\end{textblock} +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] + +{\lstset{language=Java}\fontsize{7}{8}\selectfont +\texttt{\lstinputlisting{URLReader.java}}} + +\only<2>{ +\begin{textblock}{5}(12,2) +\includegraphics[scale=0.50]{../pics/skeleton.jpg}\\ +\end{textblock}} +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + \end{document} %%% Local Variables: