progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 11 Nov 2019 13:24:12 +0000
changeset 316 8b57dd326a91
parent 310 335079d938aa
child 317 607ceabeeffc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     1
// Scala Lecture 2
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     2
//=================
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     3
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     4
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     5
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
     6
// The Option Type
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
     7
//=================
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
     8
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
     9
// in Java, if something unusually happens, you return null or 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    10
// raise an exception
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    11
//
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    12
//in Scala you use Options instead
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    13
//   - if the value is present, you use Some(value)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    14
//   - if no value is present, you use None
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    15
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    16
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    17
List(7,2,3,4,5,6).find(_ < 4)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    18
List(5,6,7,8,9).find(_ < 4)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    19
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    20
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    21
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    22
// better error handling with Options (no exceptions)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    23
//
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    24
//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    25
//
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    26
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    27
import scala.util._
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    28
import io.Source
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    29
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    30
val my_url = "https://nms.kcl.ac.uk/christian.urban/"
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    31
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    32
Source.fromURL(my_url).mkString
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    33
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    34
Try(Source.fromURL(my_url).mkString).getOrElse("")
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    35
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    36
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    37
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    38
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    39
// the same for files
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    40
Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    41
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    42
// how to implement a function for reading something from files...
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    43
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    44
def get_contents(name: String) : List[String] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    45
  Source.fromFile(name).getLines.toList
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    46
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    47
get_contents("test.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    48
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    49
// slightly better - return Nil
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    50
def get_contents(name: String) : List[String] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    51
  Try(Source.fromFile(name).getLines.toList).getOrElse(List())
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    52
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    53
get_contents("text.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    54
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    55
// much better - you record in the type that things can go wrong 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    56
def get_contents(name: String) : Option[List[String]] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    57
  Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    58
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    59
get_contents("text.txt")
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    60
get_contents("test.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    61
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    62
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    63
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    64
// For-Comprehensions Again
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    65
//==========================
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    66
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    67
// the first produces a result, while the second does not
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    68
for (n <- List(1, 2, 3, 4, 5)) yield n * n
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    69
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    70
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    71
for (n <- List(1, 2, 3, 4, 5)) println(n)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    72
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    73
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    74
// String Interpolations
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    75
//=======================
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    76
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    77
val n = 3
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    78
println("The square of " + n + " is " + square(n) + ".")
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    79
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    80
println(s"The square of ${n} is ${square(n)}.")
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    81
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    82
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    83
// helpful for debugging purposes
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    84
//
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    85
//         "The most effective debugging tool is still careful thought, 
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    86
//          coupled with judiciously placed print statements."
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    87
//                   — Brian W. Kernighan, in Unix for Beginners (1979)
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    88
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    89
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    90
def gcd_db(a: Int, b: Int) : Int = {
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    91
  println(s"Function called with ${a} and ${b}.")
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    92
  if (b == 0) a else gcd_db(b, a % b)
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    93
}
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    94
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    95
gcd_db(48, 18)
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    96
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    97
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    98
// Asserts/Testing
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    99
//=================
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   100
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   101
assert(gcd(48, 18) == 6)
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   102
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   103
assert(gcd(48, 18) == 5, "The gcd test failed")
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   104
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   105
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   106
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   107
// Higher-Order Functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   108
//========================
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   109
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   110
// functions can take functions as arguments
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   111
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   112
def even(x: Int) : Boolean = x % 2 == 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   113
def odd(x: Int) : Boolean = x % 2 == 1
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   114
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   115
val lst = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   116
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   117
lst.filter(x => even(x))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   118
lst.filter(even(_))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   119
lst.filter(even)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   120
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   121
lst.count(even)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   122
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   123
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   124
lst.find(even)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   125
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   126
val ps = List((3, 0), (3, 2), (4, 2), (2, 2), (2, 0), (1, 1), (1, 0))
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   127
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   128
lst.sortWith(_ > _)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   129
lst.sortWith(_ < _)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   130
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   131
def lex(x: (Int, Int), y: (Int, Int)) : Boolean = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   132
  if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   133
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   134
ps.sortWith(lex)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   135
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   136
ps.sortBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   137
ps.sortBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   138
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   139
ps.maxBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   140
ps.maxBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   141
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   142
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   143
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   144
// maps (lower-case)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   145
//===================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   146
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   147
def double(x: Int): Int = x + x
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   148
def square(x: Int): Int = x * x
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   149
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   150
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   151
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   152
val lst = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   153
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   154
lst.map(x => (double(x), square(x)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   155
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   156
lst.map(square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   157
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   158
// this is actually how for-comprehensions 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   159
// defined as in Scala
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   160
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   161
lst.map(n => square(n))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   162
for (n <- lst) yield square(n)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   163
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   164
// this can be iterated
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   165
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   166
lst.map(square).filter(_ > 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   167
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   168
lst.map(square).filter(_ > 4).map(square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   169
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   170
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   171
// lets define our own functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   172
// type of functions, for example f: Int => Int
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   173
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   174
lst.tail
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   175
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   176
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   177
  if (lst == Nil) Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   178
  else f(lst.head) :: my_map_int(lst.tail, f)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   179
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   180
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   181
my_map_int(lst, square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   182
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   183
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   184
// same function using pattern matching: a kind
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   185
// of switch statement on steroids (see more later on)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   186
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   187
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = lst match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   188
  case Nil => Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   189
  case x::xs => f(x)::my_map_int(xs, f)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   190
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   191
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   192
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   193
// other function types
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   194
//
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   195
// f1: (Int, Int) => Int
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   196
// f2: List[String] => Option[Int]
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   197
// ... 
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   198
val lst = (1 to 10).toList
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   199
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   200
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   201
  case Nil => 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   202
  case x::xs => f(x) + sumOf(f, xs)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   203
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   205
def sum_squares(lst: List[Int]) = sumOf(square, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   206
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   207
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   208
sum_squares(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   209
sum_cubes(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   210
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   211
// lets try it factorial
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   212
def fact(n: Int) : Int = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   213
  if (n == 0) 1 else n * fact(n - 1)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   214
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   215
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   216
sum_fact(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   217
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   218
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   219
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   220
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   221
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   222
// Map type (upper-case)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   223
//=======================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   224
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   225
// Note the difference between map and Map
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   226
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   227
def factors(n: Int) : List[Int] =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   228
  ((1 until n).filter { divisor =>
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   229
      n % divisor == 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   230
    }).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   231
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   232
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   233
var ls = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   234
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   235
val facs = ls.map(n => (n, factors(n)))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   236
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   237
facs.find(_._1 == 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   238
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   239
// works for lists of pairs
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   240
facs.toMap
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   241
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   242
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   243
facs.toMap.get(4)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   244
facs.toMap.getOrElse(42, Nil)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   245
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   246
val facsMap = facs.toMap
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   247
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   248
val facsMap0 = facsMap + (0 -> List(1,2,3,4,5))
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   249
facsMap0.get(1)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   250
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   251
val facsMap4 = facsMap + (1 -> List(1,2,3,4,5))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   252
facsMap.get(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   253
facsMap4.get(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   254
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   255
val ls = List("one", "two", "three", "four", "five")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   256
ls.groupBy(_.length)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   257
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   258
ls.groupBy(_.length).get(2)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   259
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   260
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   261
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   262
// Option type (again)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   263
//=====================
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   264
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   265
// remember, in Java if something unusually happens, 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   266
// you return null;
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   267
//
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   268
// in Scala you use Option
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   269
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   270
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   271
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   272
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   273
List(7,2,3,4,5,6).find(_ < 4)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   274
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   275
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   276
// operations on options
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   277
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   278
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   279
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   280
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   281
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   282
Some(1).get
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   283
None.get
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   284
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   285
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   286
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   287
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   288
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   289
None.isDefined
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   290
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   291
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   292
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   293
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   294
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   295
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   296
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   297
// getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   298
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   299
val lst = List(None, Some(1), Some(2), None, Some(3))
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   300
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   301
for (x <- lst) yield x.getOrElse(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   302
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   303
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   304
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   305
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   306
// error handling with Option (no exceptions)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   307
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   308
//  Try(something).getOrElse(what_to_do_in_an_exception)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   309
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   310
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   311
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   312
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   313
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   314
Source.fromURL("""http://www.inf.ucl.ac.uk/staff/urbanc/""").mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   315
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   316
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   317
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   318
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   319
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   320
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   321
// a function that turns strings into numbers (similar to .toInt)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   322
Integer.parseInt("12u34")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   323
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   324
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   325
def get_me_an_int(s: String) : Option[Int] = 
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   326
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   327
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   328
val lst = List("12345", "foo", "5432", "bar", "x21", "456")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   329
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   330
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   331
// summing up all the numbers
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   332
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   333
lst.map(get_me_an_int).flatten.sum
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   334
lst.map(get_me_an_int).flatten.sum
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   335
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   336
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   337
lst.flatMap(get_me_an_int).map(_.toString)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   338
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   339
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   340
// This may not look any better than working with null in Java, but to
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   341
// see the value, you have to put yourself in the shoes of the
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   342
// consumer of the get_me_an_int function, and imagine you didn't
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   343
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   344
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   345
// In Java, if you didn't write this function, you'd have to depend on
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   346
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc, 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   347
// you might not know that get_me_an_int could return a null, and your 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   348
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   349
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   350
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   351
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   352
// even Scala is not immune to problems like this:
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   353
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   354
List(5,6,7,8,9).indexOf(7)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   355
List(5,6,7,8,9).indexOf(10)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   356
List(5,6,7,8,9)(-1)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   357
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   358
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   359
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   360
// Pattern Matching
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   361
//==================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   362
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   363
// A powerful tool which is supposed to come to Java in a few years
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   364
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   365
// has it for many years ;o)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   366
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   367
// The general schema:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   368
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   369
//    expression match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   370
//       case pattern1 => expression1
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   371
//       case pattern2 => expression2
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   372
//       ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   373
//       case patternN => expressionN
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   374
//    }
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   375
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   376
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   377
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   378
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   379
// remember?
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   380
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   381
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   382
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   383
def my_flatten(xs: List[Option[Int]]): List[Int] = xs match {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   384
  case Nil => Nil 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   385
  case None::rest => my_flatten(rest)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   386
  case Some(v)::foo => {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   387
      v :: my_flatten(foo)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   388
  } 
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   389
}
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   390
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   391
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   392
// another example
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   393
def get_me_a_string(n: Int): String = n match {
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   394
  case 0 | 1 | 2 => "small"
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   395
  case _ => "big"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   396
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   397
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   398
get_me_a_string(0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   399
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   400
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   401
// you can also have cases combined
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   402
def season(month: String) : String = month match {
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   403
  case "March" | "April" | "May" => "It's spring"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   404
  case "June" | "July" | "August" => "It's summer"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   405
  case "September" | "October" | "November" => "It's autumn"
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   406
  case "December" => "It's winter"
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   407
  case "January" | "February" => "It's unfortunately winter"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   408
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   409
 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   410
println(season("November"))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   411
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   412
// What happens if no case matches?
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   413
println(season("foobar"))
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   414
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   415
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   416
// Days of the months
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   417
def days(month: String) : Int = month match {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   418
  case "March" | "April" | "May" => 31
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   419
  case "June" | "July" | "August" => 30
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   420
}
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   421
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   422
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   423
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   424
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   425
// Silly: fizz buzz
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   426
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   427
  case (0, 0) => "fizz buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   428
  case (0, _) => "fizz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   429
  case (_, 0) => "buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   430
  case _ => n.toString  
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   431
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   432
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   433
for (n <- 0 to 20) 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   434
 println(fizz_buzz(n))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   435
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   436
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   437
// User-defined Datatypes
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   438
//========================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   439
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   440
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   441
abstract class Colour
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   442
case object Red extends Colour 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   443
case object Green extends Colour 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   444
case object Blue extends Colour
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   445
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   446
def fav_colour(c: Colour) : Boolean = c match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   447
  case Red   => false
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   448
  case Green => true
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   449
  case Blue  => false 
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   450
}
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   451
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   452
fav_colour(Green)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   453
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   454
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   455
// ... a tiny bit more useful: Roman Numerals
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   456
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   457
abstract class RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   458
case object I extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   459
case object V extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   460
case object X extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   461
case object L extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   462
case object C extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   463
case object D extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   464
case object M extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   465
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   466
type RomanNumeral = List[RomanDigit] 
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   467
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   468
List(X,I)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   469
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   470
/*
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   471
I -> 1
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   472
II -> 2
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   473
III  -> 3
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   474
IV -> 4
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   475
V -> 5
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   476
VI -> 6
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   477
VII -> 7
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   478
VIII -> 8
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   479
IX -> 9
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   480
X -> X
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   481
*/
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   482
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   483
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   484
  case Nil => 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   485
  case M::r    => 1000 + RomanNumeral2Int(r)  
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   486
  case C::M::r => 900 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   487
  case D::r    => 500 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   488
  case C::D::r => 400 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   489
  case C::r    => 100 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   490
  case X::C::r => 90 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   491
  case L::r    => 50 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   492
  case X::L::r => 40 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   493
  case X::r    => 10 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   494
  case I::X::r => 9 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   495
  case V::r    => 5 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   496
  case I::V::r => 4 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   497
  case I::r    => 1 + RomanNumeral2Int(r)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   498
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   499
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   500
RomanNumeral2Int(List(I,V))             // 4
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   501
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   502
RomanNumeral2Int(List(V,I))             // 6
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   503
RomanNumeral2Int(List(I,X))             // 9
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   504
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   505
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   506
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   507
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   508
// another example
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   509
//=================
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   510
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   511
// Once upon a time, in a complete fictional 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   512
// country there were Persons...
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   513
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   514
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   515
abstract class Person
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   516
case object King extends Person
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   517
case class Peer(deg: String, terr: String, succ: Int) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   518
case class Knight(name: String) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   519
case class Peasant(name: String) extends Person
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   520
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   521
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   522
def title(p: Person): String = p match {
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   523
  case King => "His Majesty the King"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   524
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   525
  case Knight(name) => s"Sir ${name}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   526
  case Peasant(name) => name
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   527
}
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   528
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   529
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   530
  case (King, _) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   531
  case (Peer(_,_,_), Knight(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   532
  case (Peer(_,_,_), Peasant(_)) => true
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   533
  case (Peer(_,_,_), Clown) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   534
  case (Knight(_), Peasant(_)) => true
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   535
  case (Knight(_), Clown) => true
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   536
  case (Clown, Peasant(_)) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   537
  case _ => false
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   538
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   539
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   540
val people = List(Knight("David"), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   541
                  Peer("Duke", "Norfolk", 84), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   542
                  Peasant("Christian"), 
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   543
                  King, 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   544
                  Clown)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   545
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   546
println(people.sortWith(superior).mkString("\n"))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   547
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   548
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   549
// String interpolations as patterns
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   550
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   551
val date = "2000-01-01"
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   552
val s"$year-$month-$day" = date
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   553
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   554
def parse_date(date: String) = date match {
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   555
  case s"$year-$month-$day" => Some((year.toInt, month.toInt, day.toInt))
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   556
  case s"$day/$month/$year" => Some((year.toInt, month.toInt, day.toInt))
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   557
  case _ => None
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   558
} 
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   559
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   560
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   561
// Recursion
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   562
//===========
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   563
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   564
/* a, b, c
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   565
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   566
aa         aaa
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   567
ab         baa 
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   568
ac         caa 
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   569
ba  =>     ......
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   570
bb
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   571
bc
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   572
ca
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   573
cb
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   574
cc
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   575
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   576
*/
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   577
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   578
def perms(cs: List[Char], l: Int) : List[String] = {
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   579
  if (l == 0) List("")
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   580
  else for (c <- cs; s <- perms(cs, l - 1)) yield s"$c$s"
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   581
}
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   582
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   583
perms("abc".toList, 2)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   584
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   585
def move(from: Char, to: Char) =
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   586
  println(s"Move disc from $from to $to!")
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   587
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   588
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   589
  if (n == 0) ()
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   590
  else {
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   591
    hanoi(n - 1, from, to, via)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   592
    move(from, to)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   593
    hanoi(n - 1, via, from, to)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   594
  }
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   595
} 
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   596
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   597
hanoi(40, 'A', 'B', 'C')
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   598
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   599
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   600
// Tail Recursion
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   601
//================
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   602
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   603
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   604
def fact(n: Long): Long = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   605
  if (n == 0) 1 else n * fact(n - 1)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   606
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   607
fact(10)              //ok
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   608
fact(10000)           // produces a stackoverflow
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   609
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   610
def factT(n: BigInt, acc: BigInt): BigInt =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   611
  if (n == 0) acc else factT(n - 1, n * acc)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   612
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   613
factT(10, 1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   614
factT(100000, 1)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   615
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   616
// there is a flag for ensuring a function is tail recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   617
import scala.annotation.tailrec
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   618
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   619
@tailrec
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   620
def factT(n: BigInt, acc: BigInt): BigInt =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   621
  if (n == 0) acc else factT(n - 1, n * acc)
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   622
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   623
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   624
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   625
// for tail-recursive functions the Scala compiler
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   626
// generates loop-like code, which does not need
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   627
// to allocate stack-space in each recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   628
// call; Scala can do this only for tail-recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   629
// functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   630
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   631
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   632
// A Web Crawler / Email Harvester
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   633
//=================================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   634
//
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   635
// the idea is to look for links using the
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   636
// regular expression "https?://[^"]*" and for
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   637
// email addresses using another regex.
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   638
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   639
import io.Source
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   640
import scala.util._
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   641
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   642
// gets the first 10K of a web-page
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   643
def get_page(url: String) : String = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   644
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   645
    getOrElse { println(s"  Problem with: $url"); ""}
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   646
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   647
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   648
// regex for URLs and emails
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   649
val http_pattern = """"https?://[^"]*"""".r
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   650
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   651
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   652
//test case:
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   653
//email_pattern.findAllIn
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   654
//  ("foo bla christian@kcl.ac.uk 1234567").toList
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   655
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   656
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   657
// drops the first and last character from a string
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   658
def unquote(s: String) = s.drop(1).dropRight(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   659
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   660
def get_all_URLs(page: String): Set[String] = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   661
  http_pattern.findAllIn(page).map(unquote).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   662
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   663
// naive version of crawl - searches until a given depth,
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   664
// visits pages potentially more than once
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   665
def crawl(url: String, n: Int) : Set[String] = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   666
  if (n == 0) Set()
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   667
  else {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   668
    println(s"  Visiting: $n $url")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   669
    val page = get_page(url)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   670
    val new_emails = email_pattern.findAllIn(page).toSet
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   671
    new_emails ++ (for (u <- get_all_URLs(page)) yield crawl(u, n - 1)).flatten
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   672
  }
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   673
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   674
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   675
// some starting URLs for the crawler
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   676
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   677
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   678
crawl(startURL, 2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   679
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   680
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   681
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   682
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   683
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   684
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   685
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   686
// Sudoku
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   687
//========
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   688
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   689
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   690
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   691
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   692
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   693
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   694
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   695
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   696
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   697
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   698
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   699
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   700
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   701
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   702
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   703
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   704
type Pos = (Int, Int)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   705
val emptyValue = '.'
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   706
val maxValue = 9
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   707
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   708
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   709
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   710
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   711
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   712
def empty(game: String) = game.indexOf(emptyValue)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   713
def isDone(game: String) = empty(game) == -1 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   714
def emptyPosition(game: String) : Pos = 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   715
  (empty(game) % maxValue, empty(game) / maxValue)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   716
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   717
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   718
def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   719
def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   720
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   721
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   722
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   723
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   724
    val y0 = base(pos._2)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   725
    for (x <- (x0 until x0 + 3).toList;
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   726
         y <- (y0 until y0 + 3).toList) yield game(x + y * maxValue)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   727
}         
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   728
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   729
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   730
//get_row(game0, 0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   731
//get_row(game0, 1)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   732
//get_box(game0, (3,1))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   733
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   734
def update(game: String, pos: Int, value: Char): String = 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   735
  game.updated(pos, value)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   736
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   737
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   738
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   739
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   740
def candidates(game: String, pos: Pos): List[Char] = 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   741
  allValues.diff(toAvoid(game, pos))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   742
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   743
//candidates(game0, (0, 0))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   744
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   745
def pretty(game: String): String = 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   746
  "\n" ++ (game.sliding(maxValue, maxValue).mkString("\n"))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   747
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   748
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   749
  if (isDone(game)) List(game)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   750
  else 
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   751
    candidates(game, emptyPosition(game)).
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   752
      map(c => search(update(game, empty(game), c))).flatten
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   753
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   754
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   755
// an easy game
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   756
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   757
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   758
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   759
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   760
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   761
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   762
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   763
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   764
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   765
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   766
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   767
// a game that is in the sligtly harder category
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   768
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   769
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   770
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   771
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   772
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   773
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   774
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   775
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   776
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   777
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   778
// a game with multiple solutions
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   779
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   780
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   781
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   782
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   783
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   784
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   785
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   786
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   787
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   788
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   789
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   790
search(game0).map(pretty)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   791
search(game1).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   792
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   793
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   794
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   795
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   796
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   797
  val end = System.nanoTime()
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   798
  s"${(end - start) / i / 1.0e9} secs"
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   799
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   800
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   801
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   802
search(game3).distinct.length
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   803
time_needed(3, search(game2))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   804
time_needed(3, search(game3))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   805
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   806
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   807
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   808
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   809
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   810
// if you like verbosity, you can full-specify the literal. 
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   811
// Don't go telling that to people, though
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   812
(1 to 100).filter((x: Int) => x % 2 == 0).sum 
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   813
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   814
// As x is known to be an Int anyway, you can omit that part
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   815
(1 to 100).filter(x => x % 2 == 0).sum
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   816
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   817
// As each parameter (only x in this case) is passed only once
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   818
// you can use the wizardy placeholder syntax
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   819
(1 to 100).filter(_ % 2 == 0).sum
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   820
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   821
// But if you want to re-use your literal, you can also put it in a value
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   822
// In this case, explicit types are required because there's nothing to infer from
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   823
val isEven: (x: Int) => x % 2 == 0
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   824
(1 to 100).filter(isEven).sum