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