progs/lecture1.scala
changeset 308 e86add5a6961
parent 273 b19227660752
child 310 335079d938aa
--- a/progs/lecture1.scala	Sat Nov 02 21:23:42 2019 +0000
+++ b/progs/lecture1.scala	Sun Nov 03 14:42:17 2019 +0000
@@ -53,6 +53,10 @@
 1 :: 2 :: 3 :: Nil
 List(1, 2, 3) ::: List(4, 5, 6)
 
+// also
+List(1, 2, 3) ++ List(4, 5, 6)
+
+
 // Equality in Scala is structural
 //=================================
 val a = "Dave"
@@ -68,8 +72,8 @@
 
 n1 == n2
 
-// this applies to "concrete" values;
-// you cannot compare functions
+// this applies to "concrete" values...pretty much everything;
+// but for example you cannot compare functions (later)
 
 
 // Printing/Strings
@@ -108,6 +112,8 @@
 "hello".toList.tail
 1.toDouble
 
+1L 
+// a long
 
 // useful list methods
 
@@ -212,15 +218,6 @@
   else n + n
 }
 
-def another_silly(x: Int, y: String) : String = {
-  if (x <= 12) y
-  else x.toString
-}
-
-another_silly(2, "two")
-another_silly(12, "twelf")
-another_silly(20, "twenty")
-
 
 // If-Conditionals
 //=================
@@ -231,7 +228,6 @@
 def fact(n: Int) : Int = 
   if (n == 0) 1 else n * fact(n - 1)
 
-
 fact(5)
 fact(150)
 
@@ -304,7 +300,9 @@
 // the same for files
 Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
 
-// function reading something from files...
+
+
+// how to implement a function for reading something from files...
 
 def get_contents(name: String) : List[String] = 
   Source.fromFile(name).getLines.toList
@@ -403,7 +401,8 @@
 for (p <- lst) yield p._1 + p._2 
 
 
-// general pattern of for-yield
+// general pattern of for-yield 
+// (yield can be several lines)
 
 for (p <- ...) yield {
   // potentially complicated
@@ -443,15 +442,15 @@
 // BTW: a roundabout way of printing out a list, say
 val lst = ('a' to 'm').toList
 
-for (n <- lst) println(n)
+for (i <- (0 until lst.length)) println(lst(i))
 
-for (i <- (0 until lst.length)) println(lst(i))
 
 // Why not just? Why making your life so complicated?
 for (c <- lst) println(c)
 
 // Aside: concurrency 
-// (scala <<..parallel_collections...>> -Yrepl-class-based)
+// scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar 
+
 for (n <- (1 to 10)) println(n)
 
 import scala.collection.parallel.CollectionConverters._
@@ -467,41 +466,38 @@
   (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)
 
-val list = (1 to 1000000).toList.map(BigInt(_))
+// ...but par does not make everything faster
+
 list.sum
 list.par.sum
-list.par.reduce(_ + _)
-list.par.aggregate(BigInt(0))(_ + _, _ + _)
 
 time_needed(10, list.sum)
 time_needed(10, list.par.sum)
-time_needed(10, list.par.reduce(_ + _))
-time_needed(10, list.par.aggregate(BigInt(0))(_ + _, _ + _))
 
 
-// Just for "Fun": Mutable vs Immutable
-//=======================================
+// Mutable vs Immutable
+//======================
 //
+// Remember:
 // - no vars, no ++i, no +=
 // - no mutable data-structures (no Arrays, no ListBuffers)
 
-
+// But what the heck....
 // Q: Count how many elements are in the intersections of two sets?
 // A; IMPROPER WAY (mutable counter)
 
 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   var count = 0
-  for (x <- A; if (B contains x)) count += 1 
+  for (x <- A.par; if (B contains x)) count += 1 
   count
 }
 
-val A = (1 to 1000).toSet
-val B = (1 to 1000 by 4).toSet
+val A = (0 to 999).toSet
+val B = (0 to 999 by 4).toSet
 
 count_intersection(A, B)
 
@@ -515,7 +511,7 @@
 count_intersection2(A, B)
 
 
-//another example
+//another bad example
 def test() = {
   var cnt = 0
   for(i <- (1 to 1000000).par) cnt += 1
@@ -524,20 +520,6 @@
 
 test()
 
-//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))
-
 
 // Further Information
 //=====================