progs/lecture1.scala
changeset 32 45557ad18ea6
parent 26 a7afc2540a88
child 33 b6cb302633eb
--- 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/