progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 16 Nov 2018 02:06:03 +0000
changeset 204 9b45dd24271b
parent 192 a112e0e2325c
child 212 4bda49ec24da
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
// UNFINISHED BUSINESS from Lecture 1
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     5
//====================================
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     6
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     7
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     8
// for measuring time
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
     9
def time_needed[T](n: Int, code: => T) = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    10
  val start = System.nanoTime()
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    11
  for (i <- (0 to n)) code
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    12
  val end = System.nanoTime()
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    13
  (end - start) / 1.0e9
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    14
}
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
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    17
val list = (1 to 1000000).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    18
time_needed(10, for (n <- list) yield n + 42)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    19
time_needed(10, for (n <- list.par) yield n + 42)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    20
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    21
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    22
// Just for "Fun": Mutable vs Immutable
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    23
//=======================================
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    24
//
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    25
// - no vars, no ++i, no +=
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    26
// - no mutable data-structures (no Arrays, no ListBuffers)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    27
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    28
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    29
// Q: Count how many elements are in the intersections of two sets?
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    30
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    31
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    32
  var count = 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    33
  for (x <- A; if B contains x) count += 1 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    34
  count
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    35
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    36
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    37
val A = (1 to 1000).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    38
val B = (1 to 1000 by 4).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    39
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    40
count_intersection(A, B)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    41
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    42
// but do not try to add .par to the for-loop above
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    43
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    44
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    45
//propper parallel version
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    46
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    47
  A.par.count(x => B contains x)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    48
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    49
count_intersection2(A, B)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    50
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    51
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    52
val A = (1 to 1000000).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    53
val B = (1 to 1000000 by 4).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    54
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    55
time_needed(100, count_intersection(A, B))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    56
time_needed(100, count_intersection2(A, B))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    57
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    58
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    59
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    60
// For-Comprehensions Again
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
// the first produces a result, while the second does not
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    64
for (n <- List(1, 2, 3, 4, 5)) yield n * n
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
for (n <- List(1, 2, 3, 4, 5)) println(n)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    68
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
// Higher-Order Functions
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
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    74
// functions can take functions as arguments
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    75
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    76
def even(x: Int) : Boolean = x % 2 == 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    77
def odd(x: Int) : Boolean = x % 2 == 1
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    78
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    79
val lst = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    80
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    81
lst.filter(x => even(x))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    82
lst.filter(even(_))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    83
lst.filter(even)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    84
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    85
lst.count(even)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    86
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    87
lst.find(_ > 8)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    88
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    89
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    90
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 1), (1, 0))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    91
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    92
ps.sortBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    93
ps.sortBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    94
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    95
ps.maxBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    96
ps.maxBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    97
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    98
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    99
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   100
// maps
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   101
//=====
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   102
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   103
def square(x: Int): Int = x * x
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   104
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   105
val lst = (1 to 10).toList
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
lst.map(square)
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
// this is actually what for is defined at in Scala
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   110
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   111
lst.map(n => square(n))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   112
for (n <- lst) yield square(n)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   113
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   114
// this can be iterated
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   115
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   116
lst.map(square).filter(_ > 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   117
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   118
lst.map(square).filter(_ > 4).map(square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   119
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
// lets define our own functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   122
// type of functions, for example f: Int => Int
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   123
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   124
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   125
  if (lst == Nil) Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   126
  else f(lst.head) :: my_map_int(lst.tail, f)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   127
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   128
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   129
my_map_int(lst, square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   130
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   131
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   132
// same function using pattern matching: a kind
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   133
// of switch statement on steroids (see more later on)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   134
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   135
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
   136
  case Nil => Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   137
  case x::xs => f(x)::my_map_int(xs, f)
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
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   140
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   141
// other function types
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
// f1: (Int, Int) => Int
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   144
// f2: List[String] => Option[Int]
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   145
// ... 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   146
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   147
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   148
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   149
  case Nil => 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   150
  case x::xs => f(x) + sumOf(f, xs)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   151
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   152
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   153
def sum_squares(lst: List[Int]) = sumOf(square, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   154
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   155
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   156
sum_squares(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   157
sum_cubes(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   158
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   159
// lets try it factorial
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   160
def fact(n: Int) : Int = ...
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   161
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   162
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   163
sum_fact(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   164
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
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
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   169
// Map type
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
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   172
// Note the difference between map and Map
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   173
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   174
def factors(n: Int) : List[Int] =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   175
  ((1 until n).filter { divisor =>
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   176
      n % divisor == 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   177
    }).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   178
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
var ls = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   181
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   182
val facs = ls.map(n => (n, factors(n)))
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
facs.find(_._1 == 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   185
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   186
// works for lists of pairs
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   187
facs.toMap
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   188
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   189
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   190
facs.toMap.get(4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   191
facs.toMap.getOrElse(4, Nil)
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
val facsMap = facs.toMap
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
val facsMap0 = facsMap + (0 -> List(1,2,3,4,5))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   196
facsMap0.get(0)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   197
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   198
val facsMap4 = facsMap + (1 -> List(1,2,3,4,5))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   199
facsMap.get(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   200
facsMap4.get(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   201
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   202
val ls = List("one", "two", "three", "four", "five")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   203
ls.groupBy(_.length)
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
ls.groupBy(_.length).get(3)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   206
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   207
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   208
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   209
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   210
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   211
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   212
//in Java if something unusually happens, you return null;
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   213
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   214
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   215
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   216
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   217
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   218
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   219
List(7,2,3,4,5,6).find(_ < 4)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   220
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   221
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   222
// operations on options
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   223
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   224
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   225
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   226
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   227
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   228
Some(1).get
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   229
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   230
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   231
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   232
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   233
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
   234
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   235
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   236
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   237
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   238
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   239
// getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   240
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   241
val lst = List(None, Some(1), Some(2), None, Some(3))
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   242
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   243
for (x <- lst) yield x.getOrElse(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   244
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   245
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   246
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   247
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   248
// error handling with Option (no exceptions)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   249
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   250
//  Try(something).getOrElse(what_to_do_in_an_exception)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   251
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   252
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   253
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   254
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   255
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   256
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   257
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
   258
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   259
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
   260
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   261
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   262
// a function that turns strings into numbers (similar to .toInt)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   263
Integer.parseInt("1234")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   264
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   265
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   266
def get_me_an_int(s: String) : Option[Int] = 
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   267
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   268
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   269
val lst = List("12345", "foo", "5432", "bar", "x21", "456")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   270
for (x <- lst) yield get_me_an_int(x)
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
// summing all the numbers
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   273
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   274
lst.map(get_me_an_int)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   275
lst.map(get_me_an_int).flatten.sum
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   276
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   277
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   278
val sum = lst.flatMap(get_me_an_int).sum
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   279
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   280
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   281
// 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
   282
// 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
   283
// 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
   284
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   285
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   286
// 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
   287
// 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
   288
// 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
   289
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   290
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   291
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   292
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   293
// even Scala is not immune to problems like this:
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   294
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   295
List(5,6,7,8,9).indexOf(7)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   296
List(5,6,7,8,9).indexOf(10)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   297
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   298
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   299
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   300
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   301
// Pattern Matching
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   302
//==================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   303
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   304
// 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
   305
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   306
// has it for many years ;o)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   307
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   308
// The general schema:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   309
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   310
//    expression match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   311
//       case pattern1 => expression1
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   312
//       case pattern2 => expression2
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   313
//       ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   314
//       case patternN => expressionN
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   315
//    }
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   316
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   317
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   318
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   319
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   320
// remember?
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   321
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   322
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   323
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   324
def my_flatten(xs: List[Option[Int]]): List[Int] = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   325
  ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   326
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   327
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   328
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   329
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   330
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   331
  case Nil => Nil
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   332
  case None::xs => my_flatten(xs)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   333
  case Some(n)::xs => n::my_flatten(xs)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   334
}
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   335
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   336
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   337
// another example
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   338
def get_me_a_string(n: Int): String = n match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   339
  case 0 => "zero"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   340
  case 1 => "one"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   341
  case 2 => "two"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   342
  case _ => "many"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   343
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   344
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   345
get_me_a_string(0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   346
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   347
// you can also have cases combined
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   348
def season(month: String) = month match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   349
  case "March" | "April" | "May" => "It's spring"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   350
  case "June" | "July" | "August" => "It's summer"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   351
  case "September" | "October" | "November" => "It's autumn"
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   352
  case "December" => "It's winter"
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   353
  case "January" | "February" => "It's unfortunately winter"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   354
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   355
 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   356
println(season("November"))
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
// What happens if no case matches?
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
println(season("foobar"))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   361
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   362
// Silly: fizz buzz
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   363
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   364
  case (0, 0) => "fizz buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   365
  case (0, _) => "fizz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   366
  case (_, 0) => "buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   367
  case _ => n.toString  
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
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   370
for (n <- 0 to 20) 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   371
 println(fizz_buzz(n))
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
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   374
// User-defined Datatypes
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
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   378
abstract class Colour
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   379
case object Red extends Colour 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   380
case object Green extends Colour 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   381
case object Blue extends Colour
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   382
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   383
def fav_colour(c: Colour) : Boolean = c match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   384
  case Red   => false
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   385
  case Green => true
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   386
  case Blue  => false 
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   387
}
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   388
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   389
fav_colour(Green)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   390
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   391
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   392
// ... a bit more useful: Roman Numerals
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   393
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   394
abstract class RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   395
case object I extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   396
case object V extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   397
case object X extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   398
case object L extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   399
case object C extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   400
case object D extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   401
case object M extends RomanDigit 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   402
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   403
type RomanNumeral = List[RomanDigit] 
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   404
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   405
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   406
  case Nil => 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   407
  case M::r    => 1000 + RomanNumeral2Int(r)  
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   408
  case C::M::r => 900 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   409
  case D::r    => 500 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   410
  case C::D::r => 400 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   411
  case C::r    => 100 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   412
  case X::C::r => 90 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   413
  case L::r    => 50 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   414
  case X::L::r => 40 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   415
  case X::r    => 10 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   416
  case I::X::r => 9 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   417
  case V::r    => 5 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   418
  case I::V::r => 4 + RomanNumeral2Int(r)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   419
  case I::r    => 1 + RomanNumeral2Int(r)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   420
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   421
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   422
RomanNumeral2Int(List(I,V))             // 4
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   423
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   424
RomanNumeral2Int(List(V,I))             // 6
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   425
RomanNumeral2Int(List(I,X))             // 9
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   426
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   427
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   428
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   429
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   430
// another example
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   431
//=================
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   432
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   433
// Once upon a time, in a complete fictional country there were Persons...
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   434
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
abstract class Person
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   437
case object King extends Person
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   438
case class Peer(deg: String, terr: String, succ: Int) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   439
case class Knight(name: String) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   440
case class Peasant(name: String) extends Person
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   441
case object Clown extends Person
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   442
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   443
def title(p: Person): String = p match {
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   444
  case King => "His Majesty the King"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   445
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   446
  case Knight(name) => s"Sir ${name}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   447
  case Peasant(name) => name
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   448
}
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   449
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   450
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   451
  case (King, _) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   452
  case (Peer(_,_,_), Knight(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   453
  case (Peer(_,_,_), Peasant(_)) => true
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   454
  case (Peer(_,_,_), Clown) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   455
  case (Knight(_), Peasant(_)) => true
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   456
  case (Knight(_), Clown) => true
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   457
  case (Clown, Peasant(_)) => true
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   458
  case _ => false
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   459
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   460
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   461
val people = List(Knight("David"), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   462
                  Peer("Duke", "Norfolk", 84), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   463
                  Peasant("Christian"), 
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   464
                  King, 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   465
                  Clown)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   466
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   467
println(people.sortWith(superior(_, _)).mkString(", "))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   468
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   469
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   470
// Tail recursion
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   471
//================
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   472
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   473
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   474
def fact(n: Long): Long = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   475
  if (n == 0) 1 else n * fact(n - 1)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   476
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   477
fact(10)              //ok
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   478
fact(10000)           // produces a stackoverflow
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   479
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   480
def factT(n: BigInt, acc: BigInt): BigInt =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   481
  if (n == 0) acc else factT(n - 1, n * acc)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   482
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   483
factT(10, 1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   484
factT(100000, 1)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   485
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   486
// there is a flag for ensuring a function is tail recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   487
import scala.annotation.tailrec
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   488
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   489
@tailrec
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   490
def factT(n: BigInt, acc: BigInt): BigInt =
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   491
  if (n == 0) acc else factT(n - 1, n * acc)
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   492
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   493
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   494
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   495
// for tail-recursive functions the Scala compiler
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   496
// generates loop-like code, which does not need
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   497
// to allocate stack-space in each recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   498
// call; Scala can do this only for tail-recursive
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   499
// functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   500
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   501
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   502
// A Web Crawler 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   503
//===============
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   504
//
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   505
// the idea is to look for dead links using the
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   506
// regular expression "https?://[^"]*"
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   507
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   508
import io.Source
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   509
import scala.util._
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   510
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   511
// gets the first 10K of a web-page
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   512
def get_page(url: String) : String = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   513
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   514
    getOrElse { println(s"  Problem with: $url"); ""}
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   515
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   516
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   517
// regex for URLs and emails
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   518
val http_pattern = """"https?://[^"]*"""".r
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   519
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
   520
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   521
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   522
// drops the first and last character from a string
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   523
def unquote(s: String) = s.drop(1).dropRight(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   524
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   525
def get_all_URLs(page: String): Set[String] = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   526
  http_pattern.findAllIn(page).map(unquote).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   527
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   528
// naive version of crawl - searches until a given depth,
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   529
// visits pages potentially more than once
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   530
def crawl(url: String, n: Int) : Set[String] = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   531
  if (n == 0) Set()
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   532
  else {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   533
    println(s"  Visiting: $n $url")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   534
    val page = get_page(url)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   535
    val new_emails = email_pattern.findAllIn(page).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   536
    new_emails ++ (for (u <- get_all_URLs(page).par) yield crawl(u, n - 1)).flatten
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   537
  }
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   538
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   539
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   540
// some starting URLs for the crawler
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   541
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   542
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   543
crawl(startURL, 2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   544
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   545
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   546
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   547
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   548
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   549
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   550
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   551
// Sudoku
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   552
//========
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   553
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   554
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   555
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   556
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   557
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   558
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   559
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   560
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   561
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   562
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   563
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   564
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   565
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   566
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   567
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   568
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   569
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   570
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   571
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   572
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   573
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   574
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   575
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   576
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   577
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   578
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   579
def empty(game: String) = game.indexOf(EmptyValue)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   580
def isDone(game: String) = empty(game) == -1 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   581
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   582
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   583
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   584
def get_row(game: String, y: Int) = indexes.map(col => game(y * MaxValue + col))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   585
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
   586
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   587
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   588
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   589
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   590
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   591
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   592
    (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   593
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   594
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   595
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   596
//get_row(game0, 0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   597
//get_row(game0, 1)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   598
//get_box(game0, (3,1))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   599
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   600
def update(game: String, pos: Int, value: Char): String = game.updated(pos, value)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   601
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   602
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   603
  (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
   604
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   605
def candidates(game: String, pos: Pos): List[Char] = allValues diff toAvoid(game,pos)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   606
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   607
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   608
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   609
def pretty(game: String): String = "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   610
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   611
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   612
  if (isDone(game)) List(game)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   613
  else 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   614
    candidates(game, emptyPosition(game)).map(c => search(update(game, empty(game), c))).toList.flatten
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   615
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   616
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   617
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   618
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   619
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   620
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   621
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   622
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   623
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   624
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   625
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   626
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   627
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   628
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   629
// game that is in the hard category
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   630
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   631
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   632
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   633
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   634
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   635
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   636
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   637
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   638
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   639
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   640
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   641
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   642
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   643
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   644
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   645
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   646
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   647
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   648
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   649
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   650
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   651
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   652
search(game0).map(pretty)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   653
search(game1).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   654
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   655
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   656
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   657
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   658
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   659
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   660
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   661
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   662
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   663
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   664
search(game3).distinct.length
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   665
time_needed(3, search(game2))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   666
time_needed(3, search(game3))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   667
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   668
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   669
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   670
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   671