--- a/progs/lecture1.scala Wed Jul 24 15:18:44 2019 +0100
+++ b/progs/lecture1.scala Thu Aug 01 09:48:34 2019 +0100
@@ -37,12 +37,13 @@
List(1,2,3,1)
Set(1,2,3,1)
+// ranges
1 to 10
(1 to 10).toList
(1 until 10).toList
-// an element in a list
+// picking an element in a list
val lst = List(1, 2, 3, 1)
lst(0)
lst(2)
@@ -52,8 +53,8 @@
1 :: 2 :: 3 :: Nil
List(1, 2, 3) ::: List(4, 5, 6)
-// Equality is structural
-//========================
+// Equality in Scala is structural
+//=================================
val a = "Dave"
val b = "Dave"
@@ -76,13 +77,16 @@
println("test")
-val tst = "This is a " + "test\n"
+val tst = "This is a " ++ "test"
+print(tst)
println(tst)
val lst = List(1,2,3,1)
println(lst.toString)
+
+println(lst.mkString)
println(lst.mkString(","))
println(lst.mkString(", "))
@@ -90,6 +94,9 @@
// some methods take more than one argument
println(lst.mkString("{", ",", "}"))
+// (in this case .mkString can take no, one,
+// or three arguments...this has to do with
+// default arguments)
// Conversion methods
@@ -97,6 +104,7 @@
List(1,2,3,1).toString
List(1,2,3,1).toSet
+
"hello".toList.tail
1.toDouble
@@ -119,27 +127,31 @@
"abcdefg".startsWith("abc")
-// Types (slide)
-//===============
+// Types (see slide)
+//===================
/* Scala is a strongly typed language
- * some base types
+ * base types
Int, Long, BigInt, Float, Double
String, Char
- Boolean
+ Boolean...
- * some compound types
+ * compound types
- List[Int],
+ List[Int]
Set[Double]
Pairs: (Int, String)
List[(BigInt, String)]
Option[Int]
+
+ * user-defined types (later)
+
*/
+// you can make the type of a value explicit
val name: String = "leo"
@@ -153,6 +165,7 @@
// required: Double
// math.sqrt("64")
+
// Pairs/Tuples
//==============
@@ -174,7 +187,11 @@
def square(x: Int) : Int = x * x
def str(x: Int) : String = x.toString
+
+incr(3)
+double(4)
square(6)
+str(3)
// The general scheme for a function: you have to give a type
@@ -195,12 +212,21 @@
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
//=================
// - 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)
@@ -250,7 +276,7 @@
// Option type
//=============
-//in Java if something unusually happens, you return null
+//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)
@@ -269,7 +295,7 @@
import scala.util._
import io.Source
-val my_url = "https://nms.imperial.ac.uk/christian.urban/"
+val my_url = "https://nms.kcl.ac.uk/christian.urban/"
Source.fromURL(my_url).mkString
@@ -299,7 +325,7 @@
Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
get_contents("text.txt")
-
+get_contents("test.txt")
// String Interpolations
@@ -346,6 +372,8 @@
m <- (1 to 10).toList) yield m * n
+// you can assign the result of a for-comprehension
+// to a value
val mult_table =
for (n <- (1 to 10).toList;
m <- (1 to 10).toList) yield m * n
@@ -426,10 +454,11 @@
for (c <- lst) println(c)
// Aside: concurrency
-// (ONLY WORKS OUT-OF-THE-BOX 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)
+// (scala <<..parallel_collections...>> -Yrepl-class-based)
for (n <- (1 to 10)) println(n)
+
+import scala.collection.parallel.CollectionConverters._
+
for (n <- (1 to 10).par) println(n)
@@ -456,6 +485,7 @@
// 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