progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 09 Nov 2018 01:08:43 +0000
changeset 200 01ee4b576eb2
parent 192 a112e0e2325c
child 204 9b45dd24271b
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
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     4
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     5
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     6
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     7
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
     8
//in Java if something unusually happens, you return null;
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     9
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    10
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    11
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    12
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    13
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    14
List(7,2,3,4,5,6).find(_ < 4)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    15
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    16
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    17
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    18
// Values in types
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    19
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    20
// Boolean: 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    21
// Int: 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    22
// String: 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    23
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    24
// Option[String]:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    25
//   
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    26
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    27
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    28
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    29
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    30
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    31
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    32
Some(1).get
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    33
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    34
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    35
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    36
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    37
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
    38
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    39
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    40
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    41
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    42
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    43
// getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    44
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    45
val lst = List(None, Some(1), Some(2), None, Some(3))
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    46
for (x <- lst) yield x.getOrElse(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    47
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    48
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    49
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    50
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    51
// error handling with Option (no exceptions)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    52
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    53
//  Try(something).getOrElse(what_to_do_in_an_exception)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    54
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    55
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    56
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    57
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    58
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    59
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    60
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
    61
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    62
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
    63
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    64
// a function that turns strings into numbers
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    65
Integer.parseInt("12u34")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    66
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    67
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    68
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    69
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    70
val lst = List("12345", "foo", "5432", "bar", "x21")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    71
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    72
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    73
// summing all the numbers
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    74
val sum = lst.flatMap(get_me_an_int(_)).sum
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    75
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    76
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    77
// 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
    78
// 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
    79
// 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
    80
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    82
// 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
    83
// 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
    84
// 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
    85
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    86
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    87
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    88
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    89
// even Scala is not immune to problems like this:
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    90
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    91
List(5,6,7,8,9).indexOf(7)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    92
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    93
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    94
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    95
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    96
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    97
// Type abbreviations
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    98
//====================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
    99
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   100
// some syntactic convenience
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   101
type Pos = (int, Int)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   102
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   103
type Board = List[List[Int]]
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   104
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   105
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   106
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   107
// Implicits
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   108
//===========
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   109
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   110
// for example adding your own methods to Strings:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   111
// imagine you want to increment strings, like
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   112
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   113
//     "HAL".increment
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   114
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   115
// you can avoid ugly fudges, like a MyString, by
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   116
// using implicit conversions
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   117
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   118
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   119
implicit class MyString(s: String) {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   120
  def increment = for (c <- s) yield (c + 1).toChar 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   121
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   122
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   123
"HAL".increment
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   124
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   125
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   126
// No return in Scala
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   127
//====================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   128
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   129
//You should not use "return" in Scala:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   130
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   131
// A return expression, when evaluated, abandons the 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   132
// current computation and returns to the caller of the 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   133
// function in which return appears."
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   134
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   135
def sq1(x: Int): Int = x * x
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   136
def sq2(x: Int): Int = return x * x
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   137
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   138
def sumq(ls: List[Int]): Int = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   139
  (for (x <- ls) yield (return x * x)).sum[Int]
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   140
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   141
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   142
sumq(List(1,2,3,4))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   143
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   144
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   145
// last expression in a function is the return statement
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   146
def square(x: Int): Int = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   147
  println(s"The argument is ${x}.")
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   148
  x * x
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   149
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   150
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   151
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   152
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   153
// Pattern Matching
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   154
//==================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   155
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   156
// 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
   157
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   158
// has it for many years ;o)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   159
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   160
// The general schema:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   161
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   162
//    expression match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   163
//       case pattern1 => expression1
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   164
//       case pattern2 => expression2
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   165
//       ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   166
//       case patternN => expressionN
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   167
//    }
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   168
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   169
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   170
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   171
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   172
// remember
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   173
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   174
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   175
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   176
def my_flatten(xs: List[Option[Int]]): List[Int] = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   177
  ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   178
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   179
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   180
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   181
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   182
  case Nil => Nil
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   183
  case None::xs => my_flatten(xs)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   184
  case Some(n)::xs => n::my_flatten(xs)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   185
}
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   186
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   187
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   188
// another example
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   189
def get_me_a_string(n: Int): String = n match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   190
  case 0 => "zero"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   191
  case 1 => "one"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   192
  case 2 => "two"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   193
  case _ => "many"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   194
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   195
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   196
get_me_a_string(0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   197
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   198
// you can also have cases combined
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   199
def season(month: String) = month match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   200
  case "March" | "April" | "May" => "It's spring"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   201
  case "June" | "July" | "August" => "It's summer"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   202
  case "September" | "October" | "November" => "It's autumn"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   203
  case "December" | "January" | "February" => "It's winter"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   204
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   205
 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   206
println(season("November"))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   207
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   208
// What happens if no case matches?
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   209
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   210
println(season("foobar"))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   211
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   212
// fizz buzz
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   213
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   214
  case (0, 0) => "fizz buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   215
  case (0, _) => "fizz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   216
  case (_, 0) => "buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   217
  case _ => n.toString  
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   218
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   219
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   220
for (n <- 0 to 20) 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   221
 println(fizz_buzz(n))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   222
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   223
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   224
// User-defined Datatypes
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   225
//========================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   226
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   227
abstract class Tree
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   228
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   229
case class Leaf() extends Tree
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   230
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   231
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   232
def insert(tr: Tree, n: Int): Tree = tr match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   233
  case Leaf() => Node(n, Leaf(), Leaf())
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   234
  case Node(m, left, right) => 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   235
    if (n == m) Node(m, left, right) 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   236
    else if (n < m) Node(m, insert(left, n), right)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   237
    else Node(m, left, insert(right, n))
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   238
}
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   239
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   240
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   241
val t1 = Node(4, Node(2, Leaf(), Leaf()), Node(7, Leaf(), Leaf()))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   242
insert(t1, 3)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   243
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   244
def depth(tr: Tree): Int = tr match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   245
  case Leaf() => 0
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   246
  case Node(_, left, right) => 1 + List(depth(left), depth(right)).max
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   247
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   248
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   249
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   250
def balance(tr: Tree): Int = tr match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   251
  case Leaf() => 0
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   252
  case Node(_, left, right) => depth(left) - depth(right)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   253
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   254
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   255
balance(insert(t1, 3))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   256
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   257
// another example
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   258
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   259
abstract class Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   260
case class King() extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   261
case class Peer(deg: String, terr: String, succ: Int) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   262
case class Knight(name: String) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   263
case class Peasant(name: String) extends Person
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   264
case class Clown() extends Person
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   265
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   266
def title(p: Person): String = p match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   267
  case King() => "His Majesty the King"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   268
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   269
  case Knight(name) => s"Sir ${name}"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   270
  case Peasant(name) => name
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   271
}
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   272
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   273
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   274
  case (King(), _) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   275
  case (Peer(_,_,_), Knight(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   276
  case (Peer(_,_,_), Peasant(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   277
  case (Peer(_,_,_), Clown()) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   278
  case (Knight(_), Peasant(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   279
  case (Knight(_), Clown()) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   280
  case (Clown(), Peasant(_)) => true
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   281
  case _ => false
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   282
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   283
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   284
val people = List(Knight("David"), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   285
                  Peer("Duke", "Norfolk", 84), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   286
                  Peasant("Christian"), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   287
                  King(), 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   288
                  Clown())
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   289
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   290
println(people.sortWith(superior(_, _)).mkString(", "))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   291
173
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   292
2de1f79dedf0 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   293
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   294
// Higher-Order Functions
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   295
//========================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   296
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   297
// functions can take functions as arguments
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   298
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   299
val lst = (1 to 10).toList
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   300
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   301
def even(x: Int): Boolean = x % 2 == 0
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   302
def odd(x: Int): Boolean = x % 2 == 1
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   303
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   304
lst.filter(x => even(x))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   305
lst.filter(even(_))
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   306
lst.filter(even)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   307
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   308
lst.find(_ > 8)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   309
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   310
def square(x: Int): Int = x * x
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   311
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   312
lst.map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   313
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   314
lst.map(square).filter(_ > 4)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   315
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   316
lst.map(square).filter(_ > 4).map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   317
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   318
// in my collatz.scala
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   319
//(1 to bnd).map(i => (collatz(i), i)).maxBy(_._1)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   320
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   321
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   322
// type of functions, for example f: Int => Int
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   323
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   324
def my_map_int(lst: List[Int], f: Int => Int): List[Int] = lst match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   325
  case Nil => Nil
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   326
  case x::xs => f(x)::my_map_int(xs, f)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   327
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   328
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   329
my_map_int(lst, square)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   330
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   331
// other function types
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   332
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   333
// f1: (Int, Int) => Int
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   334
// f2: List[String] => Option[Int]
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   335
// ... 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   336
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   337
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   338
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   339
  case Nil => 0
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   340
  case x::xs => f(x) + sumOf(f, xs)
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   341
}
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   342
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   343
def sum_squares(lst: List[Int]) = sumOf(square, lst)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   344
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   345
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   346
sum_squares(lst)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   347
sum_cubes(lst)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   348
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   349
// lets try it factorial
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   350
def fact(n: Int): Int = ...
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   351
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   352
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   353
sum_fact(lst)
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   354
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   355
// Avoid being mutable
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   356
//=====================
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   357
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   358
// a student showed me...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   359
import scala.collection.mutable.ListBuffer
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   360
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   361
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   362
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   363
def collatz_max(bnd: Long): (Long, Long) = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   364
  val colNos = ListBuffer[(Long, Long)]()
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   365
  for (i <- (1L to bnd).toList) colNos += ((collatz(i), i))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   366
  colNos.max
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   367
}
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   368
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   369
def collatz_max(bnd: Long): (Long, Long) = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   370
  (1L to bnd).map((i) => (collatz(i), i)).maxBy(_._1)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   371
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   372
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   373
//views -> lazy collection
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   374
def collatz_max(bnd: Long): (Long, Long) = {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   375
  (1L to bnd).view.map((i) => (collatz(i), i)).maxBy(_._1)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   376
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   377
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   378
// raises a GC exception
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   379
(1 to 1000000000).filter(_ % 2 == 0).take(10).toList
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   380
// ==> java.lang.OutOfMemoryError: GC overhead limit exceeded
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   381
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   382
(1 to 1000000000).view.filter(_ % 2 == 0).take(10).toList
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   383
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   384
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   385
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   386
// Sudoku
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   387
//========
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   388
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   389
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   390
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   391
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   392
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   393
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   394
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   395
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   396
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   397
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   398
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   399
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   400
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   401
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   402
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   403
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   404
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   405
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   406
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   407
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   408
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   409
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   410
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   411
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   412
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   413
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   414
def empty(game: String) = game.indexOf(EmptyValue)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   415
def isDone(game: String) = empty(game) == -1 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   416
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   417
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   418
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   419
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
   420
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
   421
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   422
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   423
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   424
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   425
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   426
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   427
    (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
   428
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   429
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   430
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   431
//get_row(game0, 0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   432
//get_row(game0, 1)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   433
//get_box(game0, (3,1))
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
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
   436
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   437
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   438
  (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
   439
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   440
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
   441
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   442
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   443
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   444
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
   445
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   446
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   447
  if (isDone(game)) List(game)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   448
  else 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   449
    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
   450
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   451
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   452
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   453
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   454
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   455
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   456
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   457
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   458
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   459
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   460
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   461
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   462
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   463
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   464
// game that is in the hard category
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   465
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   466
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   467
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   468
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   469
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   470
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   471
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   472
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   473
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   474
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   475
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   476
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   477
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   478
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   480
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   481
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   482
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   484
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   485
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   486
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   487
search(game0).map(pretty)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   488
search(game1).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   489
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   490
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   491
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   492
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   493
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   494
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   495
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   496
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   497
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   498
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   499
search(game3).distinct.length
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   500
time_needed(3, search(game2))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   501
time_needed(3, search(game3))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   502
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   503
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   504
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   505
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   506