progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 12 Nov 2019 00:41:00 +0000
changeset 318 029e2862bb4e
parent 317 607ceabeeffc
child 319 b84ea52bfd8f
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
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     4
// For-Comprehensions Again
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     5
//==========================
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     6
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     7
// the first produces a result, while the second does not
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     8
for (n <- List(1, 2, 3, 4, 5)) yield n * n
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
     9
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    10
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    11
for (n <- List(1, 2, 3, 4, 5)) println(n)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    12
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    13
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    14
// String Interpolations
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    15
//=======================
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    16
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    17
def cube(n: Int) : Int = n * n * n
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    18
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    19
val n = 3
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    20
println("The cube of " + n + " is " + cube(n) + ".")
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    21
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    22
println(s"The cube of ${n} is ${cube(n)}.")
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    23
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    24
// or even
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    25
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
    26
println(s"The cube of ${n} is ${n * n * n}.")
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    27
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    28
// helpful for debugging purposes
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    29
//
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    30
//         "The most effective debugging tool is still careful thought, 
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    31
//          coupled with judiciously placed print statements."
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    32
//                   — Brian W. Kernighan, in Unix for Beginners (1979)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    33
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    34
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    35
def gcd_db(a: Int, b: Int) : Int = {
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    36
  println(s"Function called with ${a} and ${b}.")
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    37
  if (b == 0) a else gcd_db(b, a % b)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    38
}
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    39
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    40
gcd_db(48, 18)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    41
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    42
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    43
// The Option Type
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    44
//=================
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    45
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    46
// in Java, if something unusually happens, you return null or 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    47
// raise an exception
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    48
//
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    49
//in Scala you use Options instead
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    50
//   - if the value is present, you use Some(value)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    51
//   - if no value is present, you use None
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    52
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    53
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    54
List(7,2,3,4,5,6).find(_ < 4)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    55
List(5,6,7,8,9).find(_ < 4)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    56
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
    57
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    58
// better error handling with Options (no exceptions)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    59
//
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    60
//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    61
//
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    62
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    63
import scala.util._
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    64
import io.Source
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    65
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    66
val my_url = "https://nms.kcl.ac.uk/christian.urban/"
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    67
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    68
Source.fromURL(my_url).mkString
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    69
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    70
Try(Source.fromURL(my_url).mkString).getOrElse("")
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    71
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    72
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    73
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    74
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    75
// the same for files
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    76
Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    77
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    78
// how to implement a function for reading something from files...
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    79
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    80
def get_contents(name: String) : List[String] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    81
  Source.fromFile(name).getLines.toList
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    82
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    83
get_contents("test.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    84
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    85
// slightly better - return Nil
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    86
def get_contents(name: String) : List[String] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    87
  Try(Source.fromFile(name).getLines.toList).getOrElse(List())
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    88
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    89
get_contents("text.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    90
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    91
// much better - you record in the type that things can go wrong 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    92
def get_contents(name: String) : Option[List[String]] = 
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    93
  Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    94
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    95
get_contents("text.txt")
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    96
get_contents("test.txt")
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    97
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    98
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    99
// operations on options
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   100
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   101
val lst = List(None, Some(1), Some(2), None, Some(3))
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   102
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   103
lst.flatten
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   104
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   105
Some(1).get
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   106
None.get
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   107
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   108
Some(1).isDefined
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   109
None.isDefined
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   110
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   111
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   112
val ps = List((3, 0), (4, 2), (6, 2), (2, 0), (1, 0), (1, 1))
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   113
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   114
// division where possible
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   115
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   116
for ((x, y) <- ps) yield {
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   117
  if (y == 0) None else Some(x / y)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   118
}
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   119
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   120
// getOrElse is for setting a default value
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   121
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   122
val lst = List(None, Some(1), Some(2), None, Some(3))
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   123
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   124
for (x <- lst) yield x.getOrElse(0)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   125
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   126
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   127
// a function that turns strings into numbers (similar to .toInt)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   128
Integer.parseInt("1234")
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   129
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   130
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   131
def get_me_an_int(s: String) : Option[Int] = 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   132
 Try(Some(Integer.parseInt(s))).getOrElse(None)
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   133
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   134
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   135
// This may not look any better than working with null in Java, but to
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   136
// see the value, you have to put yourself in the shoes of the
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   137
// consumer of the get_me_an_int function, and imagine you didn't
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   138
// write that function.
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   139
//
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   140
// In Java, if you didn't write this function, you'd have to depend on
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   141
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc, 
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   142
// you might not know that get_me_an_int could return null, and your 
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   143
// code could potentially throw a NullPointerException.
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   144
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   145
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   146
// even Scala is not immune to problems like this:
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   147
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   148
List(5,6,7,8,9).indexOf(7)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   149
List(5,6,7,8,9).indexOf(10)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   150
List(5,6,7,8,9)(-1)
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   151
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   152
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   153
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   154
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   155
// Higher-Order Functions
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   156
//========================
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   157
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   158
// functions can take functions as arguments
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   159
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   160
def even(x: Int) : Boolean = x % 2 == 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   161
def odd(x: Int) : Boolean = x % 2 == 1
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   162
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   163
val lst = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   164
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   165
lst.filter(even)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   166
lst.count(even)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   167
lst.find(even)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   168
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   169
lst.filter(x => x % 2 == 0)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   170
lst.filter(_ % 2 == 0)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   171
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   172
lst.sortWith(_ > _)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   173
lst.sortWith(_ < _)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   174
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   175
// but this only works when the arguments are clear, but 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   176
// not with multiple occurences
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   177
lst.find(n => odd(n) && n > 2)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   178
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   179
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   180
val ps = List((3, 0), (3, 2), (4, 2), (2, 2), (2, 0), (1, 1), (1, 0))
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   181
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   182
def lex(x: (Int, Int), y: (Int, Int)) : Boolean = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   183
  if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   184
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   185
ps.sortWith(lex)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   186
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   187
ps.sortBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   188
ps.sortBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   189
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   190
ps.maxBy(_._1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   191
ps.maxBy(_._2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   192
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   193
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   194
// maps (lower-case)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   195
//===================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   196
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   197
def double(x: Int): Int = x + x
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   198
def square(x: Int): Int = x * x
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   199
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   200
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   201
val lst = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   202
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   203
lst.map(x => (double(x), square(x)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   204
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   205
lst.map(square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   206
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   207
// this is actually how for-comprehensions 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   208
// defined as in Scala
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   209
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   210
lst.map(n => square(n))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   211
for (n <- lst) yield square(n)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   212
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   213
// this can be iterated
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   214
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   215
lst.map(square).filter(_ > 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   216
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   217
lst.map(square).filter(_ > 4).map(square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   218
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   219
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   220
// lets define our own higher-order functions
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   221
// type of functions is for example Int => Int
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   222
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   223
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   224
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   225
  if (lst == Nil) Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   226
  else f(lst.head) :: my_map_int(lst.tail, f)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   227
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   228
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   229
my_map_int(lst, square)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   230
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   231
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   232
// same function using pattern matching: a kind
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   233
// of switch statement on steroids (see more later on)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   234
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   235
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = lst match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   236
  case Nil => Nil
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   237
  case x::xs => f(x)::my_map_int(xs, f)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   238
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   239
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   240
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   241
// other function types
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   242
//
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   243
// f1: (Int, Int) => Int
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   244
// f2: List[String] => Option[Int]
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   245
// ... 
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   246
val lst = (1 to 10).toList
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   247
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   248
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   249
  case Nil => 0
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   250
  case x::xs => f(x) + sumOf(f, xs)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   251
}
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   252
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   253
def sum_squares(lst: List[Int]) = sumOf(square, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   254
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   255
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   256
sum_squares(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   257
sum_cubes(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   258
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   259
// lets try a factorial
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   260
def fact(n: Int) : Int = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   261
  if (n == 0) 1 else n * fact(n - 1)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   262
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   263
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   264
sum_fact(lst)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   265
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   266
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   267
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   268
// sometimes it is needed that you specify the type. 
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   269
(1 to 100).filter((x: Int) => x % 2 == 0).sum 
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   270
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   271
// in this case it is clear that x mist be an Int
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   272
(1 to 100).filter(x => x % 2 == 0).sum
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   273
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   274
// As each parameter (only x in this case) is passed only once
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   275
// you can use the wizardy placeholder syntax
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   276
(1 to 100).filter(_ % 2 == 0).sum
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   277
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   278
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   279
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   280
// Option Type and maps
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   281
//======================
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   282
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   283
// a function that turns strings into numbers (similar to .toInt)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   284
Integer.parseInt("12u34")
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   285
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   286
import scala.util._
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   287
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   288
def get_me_an_int(s: String) : Option[Int] = 
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   289
 Try(Some(Integer.parseInt(s))).getOrElse(None)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   290
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   291
val lst = List("12345", "foo", "5432", "bar", "x21", "456")
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   292
for (x <- lst) yield get_me_an_int(x)
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   293
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   294
// summing up all the numbers
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   295
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   296
lst.map(get_me_an_int).flatten.sum
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   297
lst.map(get_me_an_int).flatten.sum
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   298
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   299
lst.flatMap(get_me_an_int).sum
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   300
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   301
// maps on Options
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   302
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   303
get_me_an_int("1234").map(even)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   304
get_me_an_int("12u34").map(even)
317
607ceabeeffc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   305
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   306
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   307
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   308
// Map type (upper-case)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   309
//=======================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   310
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   311
// Note the difference between map and Map
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   312
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   313
def factors(n: Int) : List[Int] =
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   314
  (2 until n).toList.filter(n % _ == 0)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   315
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   316
var ls = (1 to 10).toList
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   317
val facs = ls.map(n => (n, factors(n)))
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   318
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   319
facs.find(_._1 == 4)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   320
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   321
// works for lists of pairs
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   322
facs.toMap
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   323
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   324
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   325
facs.toMap.get(4)
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   326
facs.toMap.getOrElse(42, Nil)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   327
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   328
val facsMap = facs.toMap
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   329
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   330
val facsMap0 = facsMap + (0 -> List(1,2,3,4,5))
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   331
facsMap0.get(0)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   332
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   333
val facsMap2 = facsMap + (1 -> List(1,2,3,4,5))
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   334
facsMap.get(1)
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   335
facsMap2.get(1)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   336
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   337
// groupBy function on maps
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   338
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   339
val ls = List("one", "two", "three", "four", "five")
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   340
ls.groupBy(_.length)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   341
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   342
ls.groupBy(_.length).get(3)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   343
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   344
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   345
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   346
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   347
// Pattern Matching
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   348
//==================
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   349
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   350
// A powerful tool which is supposed to come to Java in a few years
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   351
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   352
// has it for many years ;o)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   353
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   354
// The general schema:
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   355
//
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   356
//    expression match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   357
//       case pattern1 => expression1
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   358
//       case pattern2 => expression2
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   359
//       ...
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   360
//       case patternN => expressionN
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   361
//    }
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   362
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   363
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   364
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   365
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   366
// remember?
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   367
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   368
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   369
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   370
def my_flatten(xs: List[Option[Int]]): List[Int] = xs match {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   371
  case Nil => Nil 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   372
  case None::rest => my_flatten(rest)
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   373
  case Some(v)::rest => v :: my_flatten(rest)
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   374
}
58
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   375
93a2b6e4b84c updated
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   376
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   377
// another example with a default case
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   378
def get_me_a_string(n: Int): String = n match {
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   379
  case 0 | 1 | 2 => "small"
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   380
  case _ => "big"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   381
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   382
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   383
get_me_a_string(0)
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   384
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   385
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   386
// you can also have cases combined
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   387
def season(month: String) : String = month match {
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   388
  case "March" | "April" | "May" => "It's spring"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   389
  case "June" | "July" | "August" => "It's summer"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   390
  case "September" | "October" | "November" => "It's autumn"
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   391
  case "December" => "It's winter"
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   392
  case "January" | "February" => "It's unfortunately winter"
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   393
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   394
 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   395
println(season("November"))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   396
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   397
// What happens if no case matches?
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   398
println(season("foobar"))
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   399
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   400
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   401
// days of some months
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   402
def days(month: String) : Int = month match {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   403
  case "March" | "April" | "May" => 31
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   404
  case "June" | "July" | "August" => 30
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   405
}
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   406
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   407
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   408
// Silly: fizz buzz
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   409
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   410
  case (0, 0) => "fizz buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   411
  case (0, _) => "fizz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   412
  case (_, 0) => "buzz"
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   413
  case _ => n.toString  
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   414
}
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   415
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   416
for (n <- 0 to 20) 
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   417
 println(fizz_buzz(n))
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   418
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   419
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   420
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   421
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   422
// Recursion
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   423
//===========
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   424
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   425
// well-known example
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   426
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   427
def fib(n: Int) : Int = { 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   428
  if (n == 0 || n == 1) 1
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   429
   else fib(n - 1) + fib(n - 2)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   430
}
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   431
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   432
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   433
/* Say you have characters a, b, c.
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   434
   What are all the combinations of a certain length?
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   435
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   436
   All combinations of length 2:
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   437
  
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   438
     aa, ab, ac, ba, bb, bc, ca, cb, cc
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   439
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   440
   Combinations of length 3:
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   441
   
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   442
     aaa, baa, caa, and so on......
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   443
*/
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   444
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   445
def combs(cs: List[Char], l: Int) : List[String] = {
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   446
  if (l == 0) List("")
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   447
  else for (c <- cs; s <- combs(cs, l - 1)) yield s"$c$s"
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   448
}
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   449
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   450
combs("abc".toList, 2)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   451
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   452
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   453
// another well-known example
309
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   454
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   455
def move(from: Char, to: Char) =
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   456
  println(s"Move disc from $from to $to!")
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   457
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   458
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   459
  if (n == 0) ()
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   460
  else {
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   461
    hanoi(n - 1, from, to, via)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   462
    move(from, to)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   463
    hanoi(n - 1, via, from, to)
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   464
  }
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   465
} 
b192bc772613 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   466
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   467
hanoi(4, 'A', 'B', 'C')
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   468
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   469
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   470
// A Recursive Web Crawler / Email Harvester
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   471
//===========================================
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   472
//
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   473
// the idea is to look for links using the
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   474
// regular expression "https?://[^"]*" and for
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   475
// email addresses using another regex.
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   476
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   477
import io.Source
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   478
import scala.util._
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   479
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   480
// gets the first 10K of a web-page
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   481
def get_page(url: String) : String = {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   482
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   483
    getOrElse { println(s"  Problem with: $url"); ""}
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   484
}
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   485
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   486
// regex for URLs and emails
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   487
val http_pattern = """"https?://[^"]*"""".r
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   488
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   489
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   490
//test case:
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   491
//email_pattern.findAllIn
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   492
//  ("foo bla christian@kcl.ac.uk 1234567").toList
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   493
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   494
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   495
// drops the first and last character from a string
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   496
def unquote(s: String) = s.drop(1).dropRight(1)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   497
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   498
def get_all_URLs(page: String): Set[String] = 
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   499
  http_pattern.findAllIn(page).map(unquote).toSet
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   500
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   501
// naive version of crawl - searches until a given depth,
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   502
// visits pages potentially more than once
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   503
def crawl(url: String, n: Int) : Unit = {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   504
  if (n == 0) ()
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   505
  else {
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   506
    println(s"  Visiting: $n $url")
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   507
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   508
  }
147
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
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   511
// some starting URLs for the crawler
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   512
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 95
diff changeset
   513
204
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   514
crawl(startURL, 2)
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   515
9b45dd24271b updated
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   516
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   517
// a primitive email harvester
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   518
def emails(url: String, n: Int) : Set[String] = {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   519
  if (n == 0) Set()
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   520
  else {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   521
    println(s"  Visiting: $n $url")
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   522
    val page = get_page(url)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   523
    val new_emails = email_pattern.findAllIn(page).toSet
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   524
    new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   525
  }
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   526
}
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   527
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   528
emails(startURL, 3)
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   529
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   530
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   531
// if we want to explore the internet "deeper", then we
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   532
// first have to parallelise the request of webpages:
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   533
//
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   534
// scala -cp scala-parallel-collections_2.13-0.2.0.jar 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 317
diff changeset
   535
// import scala.collection.parallel.CollectionConverters._
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   536
53
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
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   539
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   540
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   541