progs/lecture2.scala
changeset 155 371acb50643d
parent 150 9a2f2a1de42b
child 167 349d706586ef
--- a/progs/lecture2.scala	Fri Nov 24 01:26:01 2017 +0000
+++ b/progs/lecture2.scala	Fri Nov 24 03:10:23 2017 +0000
@@ -346,126 +346,6 @@
 better_first_word("").map(duplicate).map(valid_msg)
 
 
-// Pattern Matching
-//==================
-
-// A powerful tool which is supposed to come to Java in a few years
-// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
-// has it for many years ;o)
-
-// The general schema:
-//
-//    expression match {
-//       case pattern1 => expression1
-//       case pattern2 => expression2
-//       ...
-//       case patternN => expressionN
-//    }
-
-
-// remember
-val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
-
-
-def my_flatten(xs: List[Option[Int]]): List[Int] = {
-  ...?
-}
-
-
-
-
-
-def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
-  case Nil => Nil
-  case None::xs => my_flatten(xs)
-  case Some(n)::xs => n::my_flatten(xs)
-}
-
-
-// another example including a catch-all pattern
-def get_me_a_string(n: Int): String = n match {
-  case 0 => "zero"
-  case 1 => "one"
-  case 2 => "two"
-  case _ => "many"
-}
-
-get_me_a_string(0)
-
-// you can also have cases combined
-def season(month: String) = month match {
-  case "March" | "April" | "May" => "It's spring"
-  case "June" | "July" | "August" => "It's summer"
-  case "September" | "October" | "November" => "It's autumn"
-  case "December" | "January" | "February" => "It's winter"
-}
- 
-println(season("November"))
-
-// What happens if no case matches?
-
-println(season("foobar"))
-
-
-// User-defined Datatypes
-//========================
-
-abstract class Colour
-case class Red() extends Colour 
-case class Green() extends Colour 
-case class Blue() extends Colour
-
-def fav_colour(c: Colour) : Boolean = c match {
-  case Red()   => false
-  case Green() => true
-  case Blue()  => false 
-}
-
-
-// actually colors can be written with "object",
-// because they do not take any arguments
-
-
-// another example
-//=================
-
-// Once upon a time, in a complete fictional country there were persons...
-
-abstract class Person
-case class King() extends Person
-case class Peer(deg: String, terr: String, succ: Int) extends Person
-case class Knight(name: String) extends Person
-case class Peasant(name: String) extends Person
-
-
-def title(p: Person): String = p match {
-  case King() => "His Majesty the King"
-  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
-  case Knight(name) => s"Sir ${name}"
-  case Peasant(name) => name
-}
-
-
-def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
-  case (King(), _) => true
-  case (Peer(_,_,_), Knight(_)) => true
-  case (Peer(_,_,_), Peasant(_)) => true
-  case (Peer(_,_,_), Clown()) => true
-  case (Knight(_), Peasant(_)) => true
-  case (Knight(_), Clown()) => true
-  case (Clown(), Peasant(_)) => true
-  case _ => false
-}
-
-val people = List(Knight("David"), 
-                  Peer("Duke", "Norfolk", 84), 
-                  Peasant("Christian"), 
-                  King(), 
-                  Clown())
-
-println(people.sortWith(superior(_, _)).mkString(", "))
-
-
 
 
 
@@ -509,24 +389,8 @@
 time_needed(10, count_intersection2(A, B))
 
 
-// Implicits (Cool Feature)
-//=========================
-//
-// For example adding your own methods to Strings:
-// Imagine you want to increment strings, like
-//
-//     "HAL".increment
-//
-// you can avoid ugly fudges, like a MyString, by
-// using implicit conversions.
 
 
-implicit class MyString(s: String) {
-  def increment = for (c <- s) yield (c + 1).toChar 
-}
-
-"HAL".increment
-
 
 
 // No returns in Scala