progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 17 Nov 2017 02:13:40 +0000
changeset 147 72f7dd1a3754
parent 95 4fa7231fede7
child 148 ead6089209ba
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
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
     5
// Overloaded math operations
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
     6
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
     7
(100 / 4)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
     8
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
     9
(100 / 3)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    10
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    11
(100.toDouble / 3.toDouble)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    12
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    13
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    14
// For-Comprehensions again
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    15
//==========================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    16
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    17
def square(n: Int) : Int = n * n
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    18
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    19
for (n <- (1 to 10).toList) yield {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    20
  val res = square(n)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    21
  res
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    22
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    23
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    24
// like in functions, the "last" item inside the yield
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    25
// will be returned; the last item is not necessarily 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    26
// the last line
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    27
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    28
for (n <- (1 to 10).toList) yield {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    29
  if (n % 2 == 0) n 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    30
  else square(n)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    31
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    32
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    33
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    34
// ...please, please do not write:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    35
val lst = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    36
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    37
for (i <- (0 until lst.length).toList) yield square(lst(i))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    38
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    39
// this is just so prone to off-by-one errors;
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    40
// write instead
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    41
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    42
for (e <- lst) yield square(e)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    43
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    44
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    45
//this works for sets as well
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    46
val st = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    47
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    48
for (e <- st) yield {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    49
  if (e < 5) e else square(e)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    50
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    51
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    52
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    53
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    54
// Side-Effects
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    55
//==============
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    56
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    57
// with only a side-effect (no list is produced),
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    58
// has no "yield"
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    59
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    60
for (n <- (1 to 10)) println(n)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    61
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    62
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    63
for (n <- (1 to 10)) {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    64
  print("The number is: ")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    65
  print(n)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    66
  print("\n")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    67
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    68
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    69
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    70
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    71
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    72
// know when to use yield and when not:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    73
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    74
for (e <- Set(1, 2, 3, 4, 5, 6, 7, 8, 9); if e < 5) yield square(e)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    75
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    76
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    77
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    78
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    79
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    80
//in Java, if something unusually happens, you return null;
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    82
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    83
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    84
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    85
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    86
List(7,2,3,4,5,6).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    87
List(5,6,7,8,9).find(_ < 4)
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
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    90
// some operations on Option's
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    91
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    92
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    93
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    94
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    95
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    96
Some(1).get
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    97
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    98
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    99
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   100
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   101
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
   102
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   103
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   104
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   105
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   106
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   107
// use .getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   108
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   109
val lst = List(None, Some(1), Some(2), None, Some(3))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   110
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   111
for (x <- lst) yield x.getOrElse(0)
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
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   114
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   115
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   116
// error handling with Options (no exceptions)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   117
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   118
//  Try(....)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   119
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   120
//  Try(something).getOrElse(what_to_do_in_an_exception)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   121
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   122
import scala.util._
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   123
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   124
Try(1 + 3)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   125
Try(9 / 0) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   126
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   127
Try(9 / 3).getOrElse(42) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   128
Try(9 / 0).getOrElse(42) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   129
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   130
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   131
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   132
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   133
val my_url = """https://nms.kcl.ac.uk/christian.urban"""
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   134
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   135
Source.fromURL(my_url).mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   136
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   137
Try(Source.fromURL(my_url).mkString).getOrElse("")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   138
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   139
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   140
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   141
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   142
// a function that turns strings into numbers
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   143
Integer.parseInt("1234")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   144
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   145
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   146
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   147
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   148
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   149
val lst = List("12345", "foo", "5432", "bar", "x21")
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   150
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
// summing all the numbers
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   154
val sum = (for (i <- lst) yield get_me_an_int(i)).flatten.sum
53
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
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   157
// 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
   158
// 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
   159
// 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
   160
// write that function.
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
// In Java, if you didn't write this function, you'd have to depend on
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   163
// the Javadoc of 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
   164
// 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
   165
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   166
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   167
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   168
// even Scala is not immune to problems like this:
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   169
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   170
List(5,6,7,8,9).indexOf(7)
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   171
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   172
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   173
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   174
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   175
// Higher-Order Functions
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   176
//========================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   177
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   178
// functions can take functions as arguments
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   179
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   180
val lst = (1 to 10).toList
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   181
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   182
def even(x: Int) : Boolean = x % 2 == 0
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   183
def odd(x: Int) : Boolean = x % 2 == 1
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   184
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   185
lst.filter(x => even(x))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   186
lst.filter(even(_))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   187
lst.filter(even)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   188
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   189
lst.find(_ > 8)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   190
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   191
// map applies a function to each element of a list
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   192
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   193
def square(x: Int): Int = x * x
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   194
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   195
lst.map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   196
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   197
lst.map(square).filter(_ > 4)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   198
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   199
lst.map(square).filter(_ > 4).map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   200
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   201
// map works for most collection types, including sets
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   202
Set(1, 3, 6).map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   203
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   204
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   205
// Why could functions as arguments be useful?
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   206
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   207
// Consider the sum between a and b:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   208
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   209
def sumInts(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   210
  if (a > b) 0 else a + sumInts(a + 1, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   211
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   212
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   213
sumInt(10, 16)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   214
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   215
// sum squares
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   216
def square(n: Int) : Int = n * n
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   217
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   218
def sumSquares(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   219
  if (a > b) 0 else square(a) + sumSquares(a + 1, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   220
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   221
sumSquares(2, 6)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   222
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   223
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   224
// sum factorials
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   225
def fact(n: Int) : Int =  
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   226
  if (n == 0) 1 else n * fact(n - 1)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   227
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   228
def sumFacts(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   229
  if (a > b) 0 else fact(a) + sumFacts(a + 1, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   230
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   231
sumFacts(2, 6)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   232
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   233
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   234
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   235
// You can see the pattern....can we simplify out work?
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   236
// The type of functions from ints to ints: Int => Int
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   237
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   238
def sum(f: Int => Int, a: Int, b: Int) : Int = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   239
  if (a > b) 0 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   240
  else f(a) + sum(f, a + 1, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   241
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   242
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   243
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   244
def sumSquares(a: Int, b: Int) : Int = sum(square, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   245
def sumFacts(a: Int, b: Int) : Int = sum(fact, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   246
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   247
// What should we do for sumInts?
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   248
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   249
def id(n: Int) : Int = n
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   250
def sumInts(a: Int, b: Int) : Int = sum(id, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   251
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   252
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   253
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   254
// Anonymous Functions: You can also write:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   255
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   256
def sumCubes(a: Int, b: Int) : Int =   sum(x => x * x * x, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   257
def sumSquares(a: Int, b: Int) : Int = sum(x => x * x, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   258
def sumInts(a: Int, b: Int) : Int    = sum(x => x, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   259
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   260
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   261
// other function types
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   262
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   263
// f1: (Int, Int) => Int
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   264
// f2: List[String] => Option[Int]
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   265
// ... 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   266
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   267
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   268
// Function Composition
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   269
//======================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   270
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   271
// How could Higher-Order Functions and Options be helpful?
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   272
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   273
def add_footer(msg: String) : String = msg ++ " - Sent from iOS"
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   274
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   275
def valid_msg(msg: String) : Boolean = msg.size <= 140
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   276
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   277
def duplicate(s: String) : String = s ++ s
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   278
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   279
// they compose nicely
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   280
valid_msg(add_footer("Hello World"))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   281
valid_msg(duplicate(add_footer("Hello World")))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   282
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   283
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   284
// first_word: let's first do it the ugly Java way using null:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   285
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   286
def first_word(msg: String) : String = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   287
  val words = msg.split(" ")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   288
  if (words(0) != "") words(0) else null
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   289
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   290
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   291
duplicate(first_word("Hello World"))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   292
duplicate(first_word(""))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   293
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   294
def extended_duplicate(s: String) : String = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   295
  if (s != null) s ++ s else null
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
extended_duplicate(first_word(""))
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
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   300
// Avoid always null!
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   301
def better_first_word(msg: String) : Option[String] = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   302
  val words = msg.split(" ")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   303
  if (words(0) != "") Some(words(0)) else None
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   304
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   305
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   306
better_first_word("Hello World").map(duplicate)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   307
better_first_word("Hello World").map(duplicate).map(duplicate).map(valid_msg)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   308
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   309
better_first_word("").map(duplicate)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   310
better_first_word("").map(duplicate).map(valid_msg)
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
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   313
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   314
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   315
// Type abbreviations
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   316
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   317
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   318
// some syntactic convenience
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   319
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   320
type Pos = (int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   321
type Board = List[List[Int]]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   322
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   323
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   324
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   325
// Implicits (Cool Feature)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   326
//=========================
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   327
//
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   328
// For example adding your own methods to Strings:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   329
// Imagine you want to increment strings, like
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   330
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   331
//     "HAL".increment
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   332
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   333
// you can avoid ugly fudges, like a MyString, by
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   334
// using implicit conversions.
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   335
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   336
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   337
implicit class MyString(s: String) {
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   338
  def increment = for (c <- s) yield (c + 1).toChar 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   339
}
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   340
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   341
"HAL".increment
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   342
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   343
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   344
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   345
// No returns in Scala
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   346
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   347
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   348
// You should not use "return" in Scala:
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   349
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   350
// A return expression, when evaluated, abandons the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   351
// current computation and returns to the caller of the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   352
// function in which return appears."
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   353
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   354
def sq1(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   355
def sq2(x: Int): Int = return x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   356
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   357
def sumq(ls: List[Int]): Int = {
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   358
  ls.map(sq1).sum[Int]
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   359
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   360
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   361
sumq(List(1, 2, 3, 4))
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   362
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   363
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   364
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   365
def sumq(ls: List[Int]): Int = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   366
  val sqs : List[Int] = for (x <- ls) yield (return x * x)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   367
  sqs.sum
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   368
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   369
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   370
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   371
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   372
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   373
// Pattern Matching
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   374
//==================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   375
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   376
// 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
   377
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   378
// has it for many years ;o)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   379
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   380
// The general schema:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   381
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   382
//    expression match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   383
//       case pattern1 => expression1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   384
//       case pattern2 => expression2
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   385
//       ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   386
//       case patternN => expressionN
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   387
//    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   388
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   389
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   390
// remember
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   391
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   392
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   393
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   394
def my_flatten(xs: List[Option[Int]]): List[Int] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   395
  ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   396
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   397
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   398
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   399
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   400
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   401
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   402
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   403
  case Nil => Nil
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   404
  case None::xs => my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   405
  case Some(n)::xs => n::my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   406
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   407
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   408
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   409
// another example
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   410
def get_me_a_string(n: Int): String = n match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   411
  case 0 => "zero"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   412
  case 1 => "one"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   413
  case 2 => "two"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   414
  case _ => "many"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   415
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   416
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   417
get_me_a_string(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   418
76
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   419
// you can also have cases combined
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   420
def season(month: String) = month match {
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   421
  case "March" | "April" | "May" => "It's spring"
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   422
  case "June" | "July" | "August" => "It's summer"
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   423
  case "September" | "October" | "November" => "It's autumn"
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   424
  case "December" | "January" | "February" => "It's winter"
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   425
}
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   426
 
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   427
println(season("November"))
bc0e0aa4dee1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 61
diff changeset
   428
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   429
// What happens if no case matches?
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   430
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   431
println(season("foobar"))
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   432
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   433
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   434
// User-defined Datatypes
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
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   437
abstract class Colour
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   438
case class Red() extends Colour 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   439
case class Green() extends Colour 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   440
case class Blue() extends Colour
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   441
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   442
def fav_colour(c: Colour) : Boolean = c match {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   443
  case Red()   => false
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   444
  case Green() => true
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   445
  case Blue()  => false 
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   446
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   447
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   448
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   449
// actually this can be written with "object"
61
8bdc44963c0c updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   450
8bdc44963c0c updated
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   451
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   452
// another example
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   453
//=================
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   454
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   455
abstract class Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   456
case class King() extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   457
case class Peer(deg: String, terr: String, succ: Int) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   458
case class Knight(name: String) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   459
case class Peasant(name: String) extends Person
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   460
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   461
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   462
def title(p: Person): String = p match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   463
  case King() => "His Majesty the King"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   464
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   465
  case Knight(name) => s"Sir ${name}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   466
  case Peasant(name) => name
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   467
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   468
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   469
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   470
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   471
  case (King(), _) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   472
  case (Peer(_,_,_), Knight(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   473
  case (Peer(_,_,_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   474
  case (Peer(_,_,_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   475
  case (Knight(_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   476
  case (Knight(_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   477
  case (Clown(), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   478
  case _ => false
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
}
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
val people = List(Knight("David"), 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   482
                  Peer("Duke", "Norfolk", 84), 
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
                  Peasant("Christian"), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   484
                  King(), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   485
                  Clown())
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   486
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   487
println(people.sortWith(superior(_, _)).mkString(", "))
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   488
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   489
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   490
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   491
95
4fa7231fede7 added link file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   492
4fa7231fede7 added link file
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   493
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   494
// Problems with mutability and parallel computations
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   495
//====================================================
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   496
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   497
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   498
  var count = 0
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   499
  for (x <- A; if (B contains x)) count += 1 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   500
  count
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   501
}
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   502
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   503
val A = (1 to 1000).toSet
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   504
val B = (1 to 1000 by 4).toSet
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   505
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   506
count_intersection(A, B)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   507
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   508
// but do not try to add .par to the for-loop above
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   509
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   510
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   511
//propper parallel version
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   512
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   513
  A.par.count(x => B contains x)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   514
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   515
count_intersection2(A, B)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   516
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   517
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   518
//for measuring time
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   519
def time_needed[T](n: Int, code: => T) = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   520
  val start = System.nanoTime()
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   521
  for (i <- (0 to n)) code
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   522
  val end = System.nanoTime()
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   523
  (end - start) / 1.0e9
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   524
}
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   525
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   526
val A = (1 to 1000000).toSet
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   527
val B = (1 to 1000000 by 4).toSet
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   528
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   529
time_needed(10, count_intersection(A, B))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   530
time_needed(10, count_intersection2(A, B))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   531
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   532
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   533
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   534
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   535
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   536
// Sudoku
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   537
//========
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   538
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   539
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   540
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   541
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   542
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   543
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   544
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   545
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   546
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   547
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   548
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   549
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   550
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   551
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   552
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   553
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   554
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   555
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   556
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   557
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   558
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   559
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   560
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   561
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   562
def empty(game: String) = game.indexOf(EmptyValue)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   563
def isDone(game: String) = empty(game) == -1 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   564
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   565
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   566
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   567
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
   568
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
   569
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   570
get_row(game0, 3)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   571
get_col(game0, 0)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   572
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   573
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   574
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   575
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   576
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   577
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   578
    (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
   579
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   580
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   581
get_box(game0, (0, 0))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   582
get_box(game0, (1, 1))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   583
get_box(game0, (2, 1))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   584
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   585
// this is not mutable!!
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   586
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
   587
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   588
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   589
  (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
   590
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   591
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
   592
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   593
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   594
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   595
def pretty(game: String): String = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   596
  "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   597
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   598
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   599
  if (isDone(game)) List(game)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   600
  else {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   601
    val cs = candidates(game, emptyPosition(game))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   602
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   603
  }
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   604
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   605
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   606
search(game0).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   607
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   608
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   609
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   610
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   611
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   612
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   613
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   614
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   615
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   616
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   617
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   618
search(game1).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   619
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   620
// game that is in the hard(er) category
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   621
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   622
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   623
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   624
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   625
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   626
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   627
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   628
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   629
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   630
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   631
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   632
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   633
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   634
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   635
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   636
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   637
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   638
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   639
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   640
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   641
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   642
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   643
search(game2).map(pretty)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   644
search(game3).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   645
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   646
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   647
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   648
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   649
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   650
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   651
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   652
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   653
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   654
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   655
search(game3).distinct.length
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   656
time_needed(1, search(game2))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   657
time_needed(1, search(game3))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   658
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   659
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   660
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   661
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   662