--- a/progs/lecture1.scala Mon Nov 04 00:51:10 2019 +0000
+++ b/progs/lecture1.scala Mon Nov 04 11:48:37 2019 +0000
@@ -34,6 +34,7 @@
// Collections
//=============
+
List(1,2,3,1)
Set(1,2,3,1)
@@ -67,10 +68,6 @@
Set(1,2,3) == Set(3,1,2)
List(1,2,3) == List(3,1,2)
-val n1 = 3 + 7
-val n2 = 5 + 5
-
-n1 == n2
// this applies to "concrete" values...pretty much everything;
// but for example you cannot compare functions (later)
@@ -82,6 +79,7 @@
println("test")
val tst = "This is a " ++ "test"
+
print(tst)
println(tst)
@@ -109,11 +107,14 @@
List(1,2,3,1).toString
List(1,2,3,1).toSet
+"hello".toList
"hello".toList.tail
+
+
1.toDouble
-1L
-// a long
+1L // a long
+1F // a float
// useful list methods
@@ -185,6 +186,7 @@
List(("one", 1), ("two", 2), ("three", 3))
+
// Function Definitions
//======================
@@ -223,7 +225,7 @@
//=================
// - Scala does not have a then-keyword
-// - !both if-else branches need to be present!
+// - !!both if-else branches need to be present!!
def fact(n: Int) : Int =
if (n == 0) 1 else n * fact(n - 1)
@@ -266,94 +268,6 @@
power(5, 5)
-// Option type
-//=============
-
-//in Java if something unusually happens, you return null or something
-//
-//in Scala you use Options instead
-// - if the value is present, you use Some(value)
-// - if no value is present, you use None
-
-
-List(7,2,3,4,5,6).find(_ < 4)
-List(5,6,7,8,9).find(_ < 4)
-
-
-
-// error handling with Options (no exceptions)
-//
-// Try(something).getOrElse(what_to_do_in_case_of_an_exception)
-//
-import scala.util._
-import io.Source
-
-val my_url = "https://nms.kcl.ac.uk/christian.urban/"
-
-Source.fromURL(my_url).mkString
-
-Try(Source.fromURL(my_url).mkString).getOrElse("")
-
-Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
-
-
-// the same for files
-Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
-
-
-
-// how to implement a function for reading something from files...
-
-def get_contents(name: String) : List[String] =
- Source.fromFile(name).getLines.toList
-
-get_contents("test.txt")
-
-// slightly better - return Nil
-def get_contents(name: String) : List[String] =
- Try(Source.fromFile(name).getLines.toList).getOrElse(List())
-
-get_contents("text.txt")
-
-// much better - you record in the type that things can go wrong
-def get_contents(name: String) : Option[List[String]] =
- Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
-
-get_contents("text.txt")
-get_contents("test.txt")
-
-
-// String Interpolations
-//=======================
-
-val n = 3
-println("The square of " + n + " is " + square(n) + ".")
-
-println(s"The square of ${n} is ${square(n)}.")
-
-
-// helpful for debugging purposes
-//
-// "The most effective debugging tool is still careful thought,
-// coupled with judiciously placed print statements."
-// — Brian W. Kernighan, in Unix for Beginners (1979)
-
-
-def gcd_db(a: Int, b: Int) : Int = {
- println(s"Function called with ${a} and ${b}.")
- if (b == 0) a else gcd_db(b, a % b)
-}
-
-gcd_db(48, 18)
-
-
-// Asserts/Testing
-//=================
-
-assert(gcd(48, 18) == 6)
-
-assert(gcd(48, 18) == 5, "The gcd test failed")
-
// For-Comprehensions (not For-Loops)
//====================================
@@ -444,10 +358,11 @@
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 -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar
@@ -520,6 +435,64 @@
test()
+// Option type
+//=============
+
+//in Java if something unusually happens, you return null or something
+//
+//in Scala you use Options instead
+// - if the value is present, you use Some(value)
+// - if no value is present, you use None
+
+
+List(7,2,3,4,5,6).find(_ < 4)
+List(5,6,7,8,9).find(_ < 4)
+
+
+
+// error handling with Options (no exceptions)
+//
+// Try(something).getOrElse(what_to_do_in_case_of_an_exception)
+//
+import scala.util._
+import io.Source
+
+val my_url = "https://nms.kcl.ac.uk/christian.urban/"
+
+Source.fromURL(my_url).mkString
+
+Try(Source.fromURL(my_url).mkString).getOrElse("")
+
+Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
+
+
+// the same for files
+Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
+
+
+
+// how to implement a function for reading something from files...
+
+def get_contents(name: String) : List[String] =
+ Source.fromFile(name).getLines.toList
+
+get_contents("test.txt")
+
+// slightly better - return Nil
+def get_contents(name: String) : List[String] =
+ Try(Source.fromFile(name).getLines.toList).getOrElse(List())
+
+get_contents("text.txt")
+
+// much better - you record in the type that things can go wrong
+def get_contents(name: String) : Option[List[String]] =
+ Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
+
+get_contents("text.txt")
+get_contents("test.txt")
+
+
+
// Further Information
//=====================
--- a/progs/lecture2.scala Mon Nov 04 00:51:10 2019 +0000
+++ b/progs/lecture2.scala Mon Nov 04 11:48:37 2019 +0000
@@ -21,6 +21,8 @@
// (needs a library and 'magic' option -Yrepl-class-based)
+
+
// Just for Fun: Mutable vs Immutable
//====================================
//
@@ -70,6 +72,38 @@
for (n <- List(1, 2, 3, 4, 5)) println(n)
+// String Interpolations
+//=======================
+
+val n = 3
+println("The square of " + n + " is " + square(n) + ".")
+
+println(s"The square of ${n} is ${square(n)}.")
+
+
+// helpful for debugging purposes
+//
+// "The most effective debugging tool is still careful thought,
+// coupled with judiciously placed print statements."
+// — Brian W. Kernighan, in Unix for Beginners (1979)
+
+
+def gcd_db(a: Int, b: Int) : Int = {
+ println(s"Function called with ${a} and ${b}.")
+ if (b == 0) a else gcd_db(b, a % b)
+}
+
+gcd_db(48, 18)
+
+
+// Asserts/Testing
+//=================
+
+assert(gcd(48, 18) == 6)
+
+assert(gcd(48, 18) == 5, "The gcd test failed")
+
+
// Higher-Order Functions
//========================