diff -r 1bde878ba6c9 -r e5c1d69cffa4 progs/lecture2.scala --- a/progs/lecture2.scala Tue Nov 17 00:34:55 2020 +0000 +++ b/progs/lecture2.scala Wed Nov 18 01:54:39 2020 +0000 @@ -1,6 +1,6 @@ // Scala Lecture 2 //================= - + // String Interpolations //======================= @@ -272,11 +272,41 @@ lst.map(square).filter(_ > 4) -(lst.map(square) +lst.map(square).find(_ > 4) +lst.map(square).find(_ > 4).map(double) + +lst.map(square) .find(_ > 4) - .map(square)) + .map(double) + + +// Option Type and maps +//====================== + +// a function that turns strings into numbers (similar to .toInt) +Integer.parseInt("12u34") + +// maps on Options + +import scala.util._ -lst.map(square).find(_ > 4) +def get_me_an_int(s: String) : Option[Int] = + Try(Some(Integer.parseInt(s))).getOrElse(None) + +get_me_an_int("12345").map(_ % 2 == 0) +get_me_an_int("12u34").map(_ % 2 == 0) + + + +val lst = List("12345", "foo", "5432", "bar", "x21", "456") +for (x <- lst) yield get_me_an_int(x) + +// summing up all the numbers + +lst.map(get_me_an_int).flatten.sum + + + // this is actually how for-comprehensions are // defined in Scala @@ -288,14 +318,14 @@ // type of functions is for example Int => Int -def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = { +def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = +{ if (lst == Nil) Nil else f(lst.head) :: my_map_int(lst.tail, f) } my_map_int(lst, square) - // same function using pattern matching: a kind // of switch statement on steroids (see more later on) @@ -306,87 +336,19 @@ } + +val biglst = (1 to 10000).toList +my_map_int(biglst, double) + +(1 to 10000000).toList.map(double) + // other function types // // f1: (Int, Int) => Int // f2: List[String] => Option[Int] // ... -val lst = (1 to 10).toList - -lst.sum - -val lst = List(1,2,3,4) - -lst.head -lst.tail - -def sumOf(f: Int => Int, lst: List[Int]): Int = -lst match { - case Nil => 0 - case x::foo => f(x) + sumOf(f, foo) -} - -def sum_squares(lst: List[Int]) = sumOf(square, lst) -def sum_cubes(lst: List[Int]) = sumOf(x => x * x * x, lst) - -sum_squares(lst) -sum_cubes(lst) - -// lets try a factorial -def fact(n: Int) : Int = - if (n == 0) 1 else n * fact(n - 1) - -def sum_fact(lst: List[Int]) = sumOf(fact, lst) -sum_fact(lst) - -// sometimes it is needed that you specify the type. -(1 to 100).filter((x: Int) => x % 2 == 0).sum - -// in this case it is clear that x must be an Int -(1 to 100).filter(x => x % 2 == 0).sum - -// When each parameter (only x in this case) is used only once -// you can use the wizardy placeholder syntax -(1 to 100).filter(_ % 2 == 0).sum - - - -// Option Type and maps -//====================== - -// a function that turns strings into numbers (similar to .toInt) -Integer.parseInt("12u34") - -import scala.util._ - -def get_me_an_int(s: String) : Option[Int] = - Try(Some(Integer.parseInt(s))).getOrElse(None) - -val lst = List("12345", "foo", "5432", "bar", "x21", "456") -for (x <- lst) yield get_me_an_int(x) - -// summing up all the numbers - -lst.map(get_me_an_int).flatten.sum -lst.map(get_me_an_int).flatten.sum - -lst.flatMap(get_me_an_int).sum - -// maps on Options - -get_me_an_int("12345").map(even) -get_me_an_int("12u34").map(even) - -def my_map_option(o: Option[Int], f : Int => Int) : Option[Int] = { -o match { - case None => None - case Some(foo) => Some(f(foo)) -}} - -my_map_option(Some(4), square) -my_map_option(None, square)