diff -r 5616b45d656f -r d19b0a50ceb9 progs/lecture5.scala --- a/progs/lecture5.scala Mon Nov 30 00:06:15 2020 +0000 +++ b/progs/lecture5.scala Mon Nov 30 03:33:30 2020 +0000 @@ -172,105 +172,6 @@ -// Polymorphic Types -//=================== - -// You do not want to write functions like contains, first, -// length and so on for every type of lists. - - -def length_string_list(lst: List[String]): Int = lst match { - case Nil => 0 - case _::xs => 1 + length_string_list(xs) -} - -def length_int_list(lst: List[Int]): Int = lst match { - case Nil => 0 - case x::xs => 1 + length_int_list(xs) -} - -length_string_list(List("1", "2", "3", "4")) -length_string_list(List(1, 2, 3, 4)) - -// you can make the function parametric in type(s) - -def length[A](lst: List[A]): Int = lst match { - case Nil => 0 - case x::xs => 1 + length(xs) -} -length[String](List("1", "2", "3", "4")) -length(List(1, 2, 3, 4)) - - -def map[A, B](lst: List[A], f: A => B): List[B] = lst match { - case Nil => Nil - case x::xs => f(x)::map(xs, f) -} - -map(List(1, 2, 3, 4), (x: Int) => x.toString) - - - -// distinct / distinctBy - -val ls = List(1,2,3,3,2,4,3,2,1) -ls.distinct - -// .minBy(_._2) -// .sortBy(_._1) - -def distinctBy[B, C](xs: List[B], - f: B => C, - acc: List[C] = Nil): List[B] = xs match { - case Nil => Nil - case x::xs => { - val res = f(x) - if (acc.contains(res) distinctBy(xs, f, acc) - else x::distinctBy(xs, f, res::acc) - } -} - -val cs = List('A', 'b', 'a', 'c', 'B', 'D', 'd') - -distinctBy(cs, (c:Char) => c.toUpper) - -// since 2.13 - -cs.distinctBy((c:Char) => c.toUpper) - - -// Type inference is local in Scala - -def id[T](x: T) : T = x - -val x = id(322) // Int -val y = id("hey") // String -val z = id(Set(1,2,3,4)) // Set[Int] - -id[+A, -B] - -// The type variable concept in Scala can get really complicated. -// -// - variance (OO) -// - bounds (subtyping) -// - quantification - -// Java has issues with this too: Java allows -// to write the following incorrect code, and -// only recovers by raising an exception -// at runtime. - -// Object[] arr = new Integer[10]; -// arr[0] = "Hello World"; - - -// Scala gives you a compile-time error, which -// is much better. - -var arr = Array[Int]() -arr(0) = "Hello World" - - // (Immutable) // Object Oriented Programming in Scala //