diff -r 3350cc06804b -r ecec79b9ab25 progs/lecture1.scala --- a/progs/lecture1.scala Thu Nov 09 16:34:08 2017 +0000 +++ b/progs/lecture1.scala Fri Nov 10 00:56:12 2017 +0000 @@ -13,7 +13,6 @@ // (you cannot reassign values: z = 9 will give an error) - // Hello World //============= @@ -97,7 +96,7 @@ "1,2,3,4,5".split(",").mkString("\n") "1,2,3,4,5".split(",3,").mkString("\n") -// Types +// Types (slide) //======= /* Scala is a strongly typed language @@ -124,16 +123,16 @@ println("""">\n<"""") /* in Java -val lyrics = "Baa, Baa, Black Sheep \n" + - "Have you any wool? \n" + - "Yes, sir, yes sir \n" + - "Three bags full" +val lyrics = "Sun dips down, the day has gone \n" + + "Witches, wolves and giants yawn \n" + + "Queen and dragon, troll and gnome \n" + + "Baddy buddies head for home" */ -val lyrics = """Baa, Baa, Black Sheep - |Have you any wool? - |Yes, sir, yes sir - |Three bags full""".stripMargin +val lyrics = """Sun dips down, the day has gone + |Witches, wolves and giants yawn + |Queen and dragon, troll and gnome + |Baddy buddies head for home""".stripMargin println(lyrics) @@ -276,25 +275,73 @@ for (n <- (1 to 10)) println(n) -// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12.0) + + + + +// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12) +// (would need to have this wrapped into a function, or +// REPL called with scala -Yrepl-class-based) for (n <- (1 to 10)) println(n) for (n <- (1 to 10).par) println(n) // for measuring time -def time_needed[T](i: Int, code: => T) = { +def time_needed[T](n: Int, code: => T) = { val start = System.nanoTime() - for (j <- 1 to i) code + for (i <- (0 to n)) code val end = System.nanoTime() - ((end - start) / i / 1.0e9) + " secs" + (end - start) / 1.0e9 } + 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) +// Problems with mutability and parallel computations +//==================================================== + +def count_intersection(A: Set[Int], B: Set[Int]) : Int = { + var count = 0 + for (x <- A; if (B contains x)) count += 1 + count +} + +val A = (1 to 1000).toSet +val B = (1 to 1000 by 4).toSet + +count_intersection(A, B) + +// but do not try to add .par to the for-loop above + + +//propper parallel version +def count_intersection2(A: Set[Int], B: Set[Int]) : Int = + A.par.count(x => B contains x) + +count_intersection2(A, B) + + +//for measuring time +def time_needed[T](n: Int, code: => T) = { + val start = System.nanoTime() + for (i <- (0 to n)) code + val end = System.nanoTime() + (end - start) / 1.0e9 +} + +val A = (1 to 1000000).toSet +val B = (1 to 1000000 by 4).toSet + +time_needed(10, count_intersection(A, B)) +time_needed(10, count_intersection2(A, B)) + + + + // Webpages //========== @@ -302,10 +349,13 @@ // obtaining a webpage val url = """https://nms.kcl.ac.uk/christian.urban/""" -val url = """http://api.postcodes.io/postcodes/CR84LQ""" Source.fromURL(url)("ISO-8859-1").mkString +// another example +//val url = """http://api.postcodes.io/postcodes/CR84LQ""" + + // a function for looking up constituency data def consty_lookup(pcode: String) : String = { val url = "http://api.postcodes.io/postcodes/" + pcode @@ -350,7 +400,7 @@ // naive version of crawl - searches until a given depth, // visits pages potentially more than once -def crawl(url: String, n: Int): Unit = { +def crawl(url: String, n: Int) : Unit = { if (n == 0) () else { println(s"Visiting: $n $url")