progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 15 Jan 2018 23:15:34 +0000
changeset 167 349d706586ef
parent 155 371acb50643d
child 173 2de1f79dedf0
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
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
     5
// the pain with overloaded math operations
147
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
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    42
for (e <- lst; if (e % 2) == 0; if (e != 4)) yield square(e)
147
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),
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    58
// for has no "yield"
147
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
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    74
val test = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    75
 for (e <- Set(1, 2, 3, 4, 5, 6, 7, 8, 9); if e < 5) yield square(e)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    76
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    77
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    78
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    79
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    80
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    82
//in Java, if something unusually happens, you return null;
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    83
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    84
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    85
//   - if no value is present, you use None
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
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    88
List(7,24,3,4,5,6).find(_ < 4)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    89
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    90
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    91
List(7,2,3,4,5,6).filter(_ < 4)
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    92
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
    93
// some operations on Option's
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    94
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    95
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    96
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    97
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    98
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
    99
Some(10).get
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   100
None.get
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   101
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   102
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   103
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   104
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   105
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
   106
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   107
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   108
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   109
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   110
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   111
// use .getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   112
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   113
val lst = List(None, Some(1), Some(2), None, Some(3))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   114
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   115
for (x <- lst) yield x.getOrElse(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   116
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   117
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   118
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   119
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   120
// error handling with Options (no exceptions)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   121
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   122
//  Try(....)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   123
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   124
//  Try(something).getOrElse(what_to_do_in_an_exception)
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
import scala.util._
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   127
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   128
Try(1 + 3)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   129
Try(9 / 0) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   130
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   131
Try(9 / 3).getOrElse(42) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   132
Try(9 / 0).getOrElse(42) 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   133
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   134
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   135
import io.Source
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
val my_url = """https://nms.kcl.ac.uk/christian.urban"""
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   138
//val my_url = """https://nms.kcl.ac.uk/christan.urban"""  // misspelled
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   139
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   140
Source.fromURL(my_url).mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   141
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   142
Try(Source.fromURL(my_url).mkString).getOrElse("")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   143
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   144
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   145
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   146
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   147
// a function that turns strings into numbers
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   148
Integer.parseInt("1234")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   149
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   150
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   154
val lst = List("12345", "foo", "5432", "bar", "x21")
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   155
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   156
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   157
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   158
// summing all the numbers
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   159
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
   160
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
// 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
   163
// 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
   164
// 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
   165
// write that function.
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
// 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
   168
// 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
   169
// 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
   170
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   171
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   172
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   173
// even Scala is not immune to problems like this:
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   174
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   175
List(5,6,7,8,9).indexOf(42)
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   176
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   177
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   178
// ... how are we supposed to know that this returns -1
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   179
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   180
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   181
// Higher-Order Functions
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   182
//========================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   183
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   184
// functions can take functions as arguments
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   185
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   186
val lst = (1 to 10).toList
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   187
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   188
def even(x: Int) : Boolean = x % 2 == 0
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   189
def odd(x: Int) : Boolean = x % 2 == 1
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   190
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   191
lst.filter(x => even(x) && odd(x))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   192
lst.filter(even(_))
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   193
lst.filter(odd && even)
147
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.find(_ > 8)
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
// map applies a function to each element of a list
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
def square(x: Int): Int = x * x
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   200
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   201
val lst = (1 to 10).toList
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   202
lst.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
lst.map(square).filter(_ > 4)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   205
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   206
lst.map(square).filter(_ > 4).map(square)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   207
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   208
// map works for most collection types, including sets
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   209
Set(1, 3, 6).map(square).filter(_ > 4)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   210
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   211
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   212
val l = List((1, 3),(2, 4),(4, 1),(6, 2))
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   213
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   214
l.map(square(_._1))
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   215
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   216
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   217
// Why are functions as arguments useful?
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   218
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   219
// Consider the sum between a and 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
def sumInts(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   222
  if (a > b) 0 else a + sumInts(a + 1, b)
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
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   225
sumInts(10, 16)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   226
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   227
// sum squares
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   228
def square(n: Int) : Int = n * n
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   229
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   230
def sumSquares(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   231
  if (a > b) 0 else square(a) + sumSquares(a + 1, b)
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
sumSquares(2, 6)
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
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   236
// sum factorials
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   237
def fact(n: Int) : Int =  
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   238
  if (n == 0) 1 else n * fact(n - 1)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   239
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   240
def sumFacts(a: Int, b: Int) : Int = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   241
  if (a > b) 0 else fact(a) + sumFacts(a + 1, b)
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
sumFacts(2, 6)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   244
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   245
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   246
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   247
// You can see the pattern....can we simplify our work?
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   248
// The type of functions from ints to ints: Int => Int
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   249
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   250
def sum(f: Int => Int, a: Int, b: Int) : Int = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   251
  if (a > b) 0 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   252
  else f(a) + sum(f, a + 1, b)
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
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 sumSquares(a: Int, b: Int) : Int = sum(square, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   257
def sumFacts(a: Int, b: Int) : Int = sum(fact, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   258
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   259
// What should we do for sumInts?
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
def id(n: Int) : Int = n
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   262
def sumInts(a: Int, b: Int) : Int = sum(id, a, b)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   263
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   264
sumInts(10, 12)
147
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
// Anonymous Functions: You can also write:
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   268
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   269
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
   270
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
   271
def sumInts(a: Int, b: Int) : Int    = sum(x => x, a, b)
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
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   274
// other function types
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   275
//
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   276
// f1: (Int, Int) => Int
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   277
// f2: List[String] => Option[Int]
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
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   280
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   281
// an aside: partial application
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   282
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   283
def add(a: Int)(b: Int) : Int = a + b
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   284
def add_abc(a: Int)(b: Int)(c: Int) : Int = a + b + c
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   285
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   286
val add2 : Int => Int = add(2)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   287
add2(5)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   288
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   289
val add2_bc : Int => Int => Int = add_abc(2) 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   290
val add2_9_c : Int => Int = add2_bc(9) 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   291
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   292
add2_9_c(10)
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   293
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   294
sum(add(2), 0, 2)
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   295
sum(add(10), 0, 2)
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   296
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   297
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   298
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   299
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   300
// some automatic timing in each evaluation
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   301
package wrappers {  
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   302
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   303
  object wrap { 
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   304
   
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   305
    def timed[R](block: => R): R = {
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   306
      val t0 = System.nanoTime()
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   307
      val result = block
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   308
      println("Elapsed time: " + (System.nanoTime - t0) + "ns")
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   309
      result
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   310
    }
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   311
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   312
    def apply[A](a: => A): A = { 
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   313
      timed(a)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   314
    } 
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   315
  }
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   316
}
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   317
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   318
$intp.setExecutionWrapper("wrappers.wrap")
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   319
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   320
// Iteration
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   321
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   322
def fib(n: Int) : Int = 
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   323
  if (n <= 1) 1 else fib(n - 1) + fib(n - 2)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   324
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   325
fib(10)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   326
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   327
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   328
Iterator.iterate((1,1)){ case (n: Int, m: Int) => (n + m, n) }.drop(9).next
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   329
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   330
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   331
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   332
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   333
// Function Composition
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   334
//======================
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   335
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   336
// How can be Higher-Order Functions and Options be helpful?
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   337
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   338
def add_footer(msg: String) : String = msg ++ " - Sent from iOS"
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   339
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   340
def valid_msg(msg: String) : Boolean = msg.size <= 140
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   341
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   342
def duplicate(s: String) : String = s ++ s
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   343
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   344
// they compose very nicely, e.g
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   345
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   346
valid_msg(add_footer("Hello World"))
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   347
valid_msg(duplicate(duplicate(add_footer("Helloooooooooooooooooo World"))))
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   348
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   349
// but not all functions do
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   350
// 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
   351
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   352
def first_word(msg: String) : String = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   353
  val words = msg.split(" ")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   354
  if (words(0) != "") words(0) else null
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   355
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   356
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   357
duplicate(first_word("Hello World"))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   358
duplicate(first_word(""))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   359
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   360
def extended_duplicate(s: String) : String = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   361
  if (s != null) s ++ s else null
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   362
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   363
extended_duplicate(first_word(""))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   364
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   365
// but this is against the rules of the game: we do not want
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   366
// to change duplicate, because first_word might return null
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   367
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   368
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   369
// Avoid always null!
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   370
def better_first_word(msg: String) : Option[String] = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   371
  val words = msg.split(" ")
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   372
  if (words(0) != "") Some(words(0)) else None
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   373
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   374
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   375
better_first_word("Hello World").map(duplicate)
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   376
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   377
better_first_word("Hello World").map(duplicate)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   378
better_first_word("").map(duplicate).map(duplicate).map(valid_msg)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   379
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   380
better_first_word("").map(duplicate)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   381
better_first_word("").map(duplicate).map(valid_msg)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   382
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   383
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   384
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   385
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   386
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   387
// Problems with mutability and parallel computations
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   388
//====================================================
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   389
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   390
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   391
  var count = 0
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   392
  for (x <- A; if (B contains x)) count += 1 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   393
  count
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   394
}
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   395
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   396
val A = (1 to 1000).toSet
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   397
val B = (1 to 1000 by 4).toSet
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   398
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   399
count_intersection(A, B)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   400
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   401
// but do not try to add .par to the for-loop above,
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   402
// otherwise you will be caught in race-condition hell.
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   403
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   404
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   405
//propper parallel version
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   406
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   407
  A.par.count(x => B contains x)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   408
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   409
count_intersection2(A, B)
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   410
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   411
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   412
//for measuring time
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   413
def time_needed[T](n: Int, code: => T) = {
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   414
  val start = System.nanoTime()
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   415
  for (i <- (0 to n)) code
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   416
  val end = System.nanoTime()
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   417
  (end - start) / 1.0e9
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   418
}
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   419
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   420
val A = (1 to 1000000).toSet
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   421
val B = (1 to 1000000 by 4).toSet
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   422
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   423
time_needed(10, count_intersection(A, B))
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   424
time_needed(10, count_intersection2(A, B))
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   425
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   426
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   427
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   428
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   429
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   430
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   431
// No returns in Scala
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   432
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   433
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   434
// You should not use "return" in Scala:
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   435
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   436
// A return expression, when evaluated, abandons the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   437
// current computation and returns to the caller of the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   438
// function in which return appears."
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   439
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   440
def sq1(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   441
def sq2(x: Int): Int = return x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   442
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   443
def sumq(ls: List[Int]): Int = {
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   444
  ls.map(sq1).sum[Int]
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   445
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   446
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   447
sumq(List(1, 2, 3, 4))
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   448
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   449
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   450
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   451
def sumq(ls: List[Int]): Int = {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   452
  val sqs : List[Int] = for (x <- ls) yield (return x * x)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   453
  sqs.sum
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   454
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   455
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   456
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   457
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   458
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   459
// Type abbreviations
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   460
//====================
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   461
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   462
// some syntactic convenience
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   463
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   464
type Pos = (int, Int)
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   465
type Board = List[List[Int]]
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   466
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   467
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   468
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   469
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   470
// Sudoku in Scala
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   471
//=================
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   472
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   473
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   474
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   475
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   476
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   477
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   478
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   480
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   481
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   482
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   484
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   485
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   486
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   487
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   488
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   489
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   490
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   491
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   492
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   493
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   494
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   495
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   496
def empty(game: String) = game.indexOf(EmptyValue)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   497
def isDone(game: String) = empty(game) == -1 
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   498
def emptyPosition(game: String) = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   499
  (empty(game) % MaxValue, empty(game) / MaxValue)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   500
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   501
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   502
def get_row(game: String, y: Int) = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   503
  indexes.map(col => game(y * MaxValue + col))
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   504
def get_col(game: String, x: Int) = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   505
  indexes.map(row => game(x + row * MaxValue))
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   506
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   507
get_row(game0, 3)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   508
get_col(game0, 0)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   509
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   510
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   511
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   512
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   513
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   514
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   515
    (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
   516
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   517
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   518
get_box(game0, (0, 0))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   519
get_box(game0, (1, 1))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   520
get_box(game0, (2, 1))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   521
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   522
// this is not mutable!!
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   523
def update(game: String, pos: Int, value: Char): String = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   524
  game.updated(pos, value)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   525
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   526
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   527
  (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
   528
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   529
def candidates(game: String, pos: Pos): List[Char] = 
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   530
  allValues.diff(toAvoid(game,pos))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   531
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   532
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   533
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   534
def pretty(game: String): String = 
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   535
  "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   536
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   537
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   538
  if (isDone(game)) List(game)
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   539
  else {
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   540
    val cs = candidates(game, emptyPosition(game))
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   541
    cs.par.map(c => search(update(game, empty(game), c))).toList.flatten
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   542
  }
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   543
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   544
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   545
search(game0).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   546
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   547
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   548
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   549
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   550
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   551
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   552
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   553
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   554
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   555
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   556
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   557
search(game1).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   558
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   559
// game that is in the hard(er) category
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   560
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   561
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   562
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   563
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   564
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   565
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   566
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   567
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   568
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   569
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   570
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   571
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   572
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   573
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   574
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   575
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   576
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   577
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   578
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   579
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   580
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   581
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   582
search(game2).map(pretty)
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   583
search(game3).map(pretty)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   584
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   585
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   586
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   587
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   588
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   589
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   590
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   591
}
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
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   594
search(game3).distinct.length
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   595
time_needed(1, search(game2))
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   596
time_needed(1, search(game3))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   597
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   598
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   599
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   600
150
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   601
//===================
9a2f2a1de42b updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   602
// the end for today