progs/lecture3.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 15 Sep 2024 12:57:59 +0100
changeset 493 244df77507c2
parent 481 e03a0100ec46
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     1
// Scala Lecture 3
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     2
//=================
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     3
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
     4
// last week:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
     5
// higher-order functions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
     6
// maps
449
d67c5f7177a6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 448
diff changeset
     7
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
     8
// - recursion
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
     9
// - Sudoku
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    10
// - string interpolations
448
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    11
// - Pattern-Matching
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    12
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    13
// A Recursive Web Crawler / Email Harvester
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    14
//===========================================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    15
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    16
// the idea is to look for links using the
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    17
// regular expression "https?://[^"]*" and for
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    18
// email addresses using another regex.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    19
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    20
import io.Source
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    21
import scala.util._
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    22
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    23
// gets the first 10K of a web-page
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    24
def get_page(url: String) : String = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    25
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    26
    getOrElse { println(s"  Problem with: $url"); ""}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    27
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    28
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    29
// regex for URLs and emails
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    30
val http_pattern = """"https?://[^"]*"""".r
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    31
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    32
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    33
//test case:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    34
//email_pattern.findAllIn
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    35
//  ("foo bla christian@kcl.ac.uk 1234567").toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    36
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    37
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    38
// drops the first and last character from a string
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    39
def unquote(s: String) = s.drop(1).dropRight(1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    40
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    41
def get_all_URLs(page: String): Set[String] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    42
  http_pattern.findAllIn(page).map(unquote).toSet
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    43
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    44
// naive version of crawl - searches until a given depth,
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    45
// visits pages potentially more than once
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    46
def crawl(url: String, n: Int) : Unit = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    47
  if (n == 0) ()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    48
  else {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    49
    println(s"  Visiting: $n $url")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    50
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    51
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    52
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    53
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    54
// some starting URLs for the crawler
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    55
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    56
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    57
crawl(startURL, 2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    58
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    59
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    60
// a primitive email harvester
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    61
def emails(url: String, n: Int) : Set[String] = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    62
  if (n == 0) Set()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    63
  else {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    64
    println(s"  Visiting: $n $url")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    65
    val page = get_page(url)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    66
    val new_emails = email_pattern.findAllIn(page).toSet
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    67
    new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    68
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    69
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    70
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    71
emails(startURL, 2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    72
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    73
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    74
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    75
// Sudoku 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    76
//========
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    77
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    78
// THE POINT OF THIS CODE IS NOT TO BE SUPER
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    79
// EFFICIENT AND FAST, just explaining exhaustive
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    80
// depth-first search
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    81
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    82
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    83
val game0 = """.14.6.3..
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    84
              |62...4..9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    85
              |.8..5.6..
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    86
              |.6.2....3
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    87
              |.7..1..5.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    88
              |5....9.6.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    89
              |..6.2..3.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    90
              |1..5...92
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    91
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    92
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    93
type Pos = (Int, Int)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    94
val EmptyValue = '.'
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    95
val MaxValue = 9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    96
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    97
def pretty(game: String): String = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    98
  "\n" + (game.grouped(MaxValue).mkString("\n"))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
    99
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   100
pretty(game0)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   101
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   102
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   103
val allValues = "123456789".toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   104
val indexes = (0 to 8).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   105
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   106
def empty(game: String) = game.indexOf(EmptyValue)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   107
def isDone(game: String) = empty(game) == -1 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   108
def emptyPosition(game: String) : Pos = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   109
  val e = empty(game)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   110
  (e % MaxValue, e / MaxValue)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   111
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   112
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   113
def get_row(game: String, y: Int) = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   114
  indexes.map(col => game(y * MaxValue + col))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   115
def get_col(game: String, x: Int) = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   116
  indexes.map(row => game(x + row * MaxValue))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   117
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   118
//get_row(game0, 0)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   119
//get_row(game0, 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   120
//get_col(game0, 0)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   121
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   122
def get_box(game: String, pos: Pos): List[Char] = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   123
    def base(p: Int): Int = (p / 3) * 3
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   124
    val x0 = base(pos._1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   125
    val y0 = base(pos._2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   126
    val ys = (y0 until y0 + 3).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   127
    (x0 until x0 + 3).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   128
      .flatMap(x => ys.map(y => game(x + y * MaxValue)))
449
d67c5f7177a6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 448
diff changeset
   129
}
d67c5f7177a6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 448
diff changeset
   130
d67c5f7177a6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 448
diff changeset
   131
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   132
//get_box(game0, (3, 1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   133
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   134
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   135
// this is not mutable!!
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   136
def update(game: String, pos: Int, value: Char): String = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   137
  game.updated(pos, value)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   138
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   139
def toAvoid(game: String, pos: Pos): List[Char] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   140
  (get_col(game, pos._1) ++ 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   141
   get_row(game, pos._2) ++ 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   142
   get_box(game, pos))
448
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   143
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   144
def candidates(game: String, pos: Pos): List[Char] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   145
  allValues.diff(toAvoid(game, pos))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   146
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   147
//candidates(game0, (0,0))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   148
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   149
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   150
def search(game: String): List[String] = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   151
  if (isDone(game)) List(game)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   152
  else {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   153
    val cs = candidates(game, emptyPosition(game))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   154
    cs.par.map(c => search(update(game, empty(game), c))).flatten.toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   155
  }
448
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   156
}
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   157
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   158
pretty(game0)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   159
search(game0).map(pretty)
448
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   160
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   161
val game1 = """23.915...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   162
              |...2..54.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   163
              |6.7......
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   164
              |..1.....9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   165
              |89.5.3.17
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   166
              |5.....6..
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   167
              |......9.5
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   168
              |.16..7...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   169
              |...329..1""".stripMargin.replaceAll("\\n", "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   170
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   171
search(game1).map(pretty)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   172
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   173
// a game that is in the hard category
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   174
val game2 = """8........
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   175
              |..36.....
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   176
              |.7..9.2..
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   177
              |.5...7...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   178
              |....457..
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   179
              |...1...3.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   180
              |..1....68
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   181
              |..85...1.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   182
              |.9....4..""".stripMargin.replaceAll("\\n", "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   183
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   184
search(game2).map(pretty)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   185
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   186
// game with multiple solutions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   187
val game3 = """.8...9743
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   188
              |.5...8.1.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   189
              |.1.......
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   190
              |8....5...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   191
              |...8.4...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   192
              |...3....6
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   193
              |.......7.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   194
              |.3.5...8.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   195
              |9724...5.""".stripMargin.replaceAll("\\n", "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   196
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   197
search(game3).map(pretty).foreach(println)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   198
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   199
// for measuring time
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   200
def time_needed[T](i: Int, code: => T) = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   201
  val start = System.nanoTime()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   202
  for (j <- 1 to i) code
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   203
  val end = System.nanoTime()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   204
  s"${(end - start) / 1.0e9} secs"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   205
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   206
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   207
time_needed(2, search(game2))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   208
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   209
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   210
// concurrency 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   211
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   212
// import scala.collection.parallel.CollectionConverters._
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   213
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   214
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   215
448
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   216
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   217
// String Interpolations
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   218
//=======================
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   219
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   220
def cube(n: Int) : Int = n * n * n
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   221
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   222
val n = 3
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   223
println("The cube of " + n + " is " + cube(n) + ".")
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   224
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   225
println(s"The cube of $n is ${cube(n)}.")
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   226
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   227
// or even
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   228
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   229
println(s"The cube of $n is ${n * n * n}.")
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   230
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   231
// helpful for debugging purposes
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   232
//
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   233
//     "The most effective debugging tool is still careful 
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   234
//          thought, coupled with judiciously placed print 
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   235
//                                             statements."
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   236
//       — Brian W. Kernighan, in Unix for Beginners (1979)
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   237
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   238
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   239
def gcd_db(a: Int, b: Int) : Int = {
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   240
  println(s"Function called with $a and $b.")
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   241
  if (b == 0) a else gcd_db(b, a % b)
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   242
}
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   243
db2a3e3287a9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   244
gcd_db(48, 18)
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   245
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 375
diff changeset
   246
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   247
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   248
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   249
// Recursion Again ;o)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   250
//====================
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   251
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   252
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   253
// another well-known example: Towers of Hanoi
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   254
//=============================================
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   255
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   256
def move(from: Char, to: Char) =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   257
  println(s"Move disc from $from to $to!")
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   258
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   259
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   260
  if (n == 0) ()
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   261
  else {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   262
    hanoi(n - 1, from, to, via)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   263
    move(from, to)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   264
    hanoi(n - 1, via, from, to)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   265
  }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   266
} 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   267
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   268
hanoi(4, 'A', 'B', 'C')
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   269
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   270
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   271
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   272
// Pattern Matching
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   273
//==================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   274
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   275
// A powerful tool which has even landed in Java during 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   276
// the last few years (https://inside.java/2021/06/13/podcast-017/).
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   277
// ...Scala already has it for many years and the concept is
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   278
// older than your friendly lecturer, that is stone old  ;o)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   279
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   280
// The general schema:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   281
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   282
//    expression match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   283
//       case pattern1 => expression1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   284
//       case pattern2 => expression2
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   285
//       ...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   286
//       case patternN => expressionN
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   287
//    }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   288
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   289
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   290
// recall
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   291
def len(xs: List[Int]) : Int = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   292
    if (xs == Nil) 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   293
    else 1 + len(xs.tail)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   294
}    
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   295
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   296
def len(xs: List[Int]) : Int = xs match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   297
    case Nil => 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   298
    case hd::tail => 1 + len(tail)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   299
}  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   300
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   301
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   302
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   303
  lst match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   304
    case Nil => Nil
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   305
    case x::xs => f(x)::my_map_int(xs, f)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   306
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   307
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   308
def my_map_option(opt: Option[Int], f: Int => Int) : Option[Int] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   309
  opt match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   310
    case None => None
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   311
    case Some(x) => Some(f(x))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   312
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   313
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   314
my_map_option(None, x => x * x)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   315
my_map_option(Some(8), x => x * x)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   316
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   317
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   318
// you can also have cases combined
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   319
def season(month: String) : String = month match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   320
  case "March" | "April" | "May" => "It's spring"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   321
  case "June" | "July" | "August" => "It's summer"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   322
  case "September" | "October" | "November" => "It's autumn"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   323
  case "December" => "It's winter"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   324
  case "January" | "February" => "It's unfortunately winter"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   325
  case _ => "Wrong month"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   326
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   327
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   328
// pattern-match on integers
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   329
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   330
def fib(n: Int) : Int = n match { 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   331
  case 0 | 1 => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   332
  case n => fib(n - 1) + fib(n - 2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   333
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   334
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   335
fib(10)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   336
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   337
// pattern-match on results
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   338
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   339
// Silly: fizz buzz
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   340
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   341
  case (0, 0) => "fizz buzz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   342
  case (0, _) => "fizz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   343
  case (_, 0) => "buzz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   344
  case _ => n.toString  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   345
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   346
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   347
for (n <- 1 to 20) 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   348
 println(fizz_buzz(n))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   349
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   350
// more interesting patterns for lists - calculate the deltas between 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   351
// elements
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   352
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   353
def delta(xs: List[Int]) : List[Int] = xs match {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   354
  case Nil => Nil
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   355
  case x::Nil => x::Nil
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   356
  case x::y::xs => (x - y)::delta(y::xs)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   357
}
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   358
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   359
delta(List(10, 7, 8, 2, 5, 10))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   360
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   361
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   362
// guards in pattern-matching
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   363
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   364
def foo(xs: List[Int]) : String = xs match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   365
  case Nil => s"this list is empty"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   366
  case x :: xs if x % 2 == 0 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   367
     => s"the first elemnt is even"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   368
  case x :: y :: rest if x == y
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   369
     => s"this has two elemnts that are the same"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   370
  case hd :: tl => s"this list is standard $hd::$tl"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   371
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   372
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   373
foo(Nil)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   374
foo(List(1,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   375
foo(List(1,2))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   376
foo(List(1,1,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   377
foo(List(2,2,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   378
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   379
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   380
// Trees
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   381
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   382
abstract class Tree
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   383
case class Leaf(x: Int) extends Tree
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   384
case class Node(s: String, left: Tree, right: Tree) extends Tree 
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   385
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   386
val lf = Leaf(20)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   387
val tr = Node("foo", Leaf(10), Leaf(23))
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   388
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   389
val lst : List[Tree] = List(lf, tr)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   390
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   391
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   392
abstract class Colour
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   393
case object Red extends Colour 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   394
case object Green extends Colour 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   395
case object Blue extends Colour
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   396
case object Yellow extends Colour
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   397
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   398
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   399
def fav_colour(c: Colour) : Boolean = c match {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   400
  case Green => true
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
   401
  case _  => false 
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   402
}
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   403
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   404
fav_colour(Blue)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   405
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   406
enum ChessPiece:
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   407
  case Queen, Rook, Bishop, Knight, Pawn
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   408
  def value = this match
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   409
    case Queen  => 9
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   410
    case Rook   => 5
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   411
    case Bishop => 3
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   412
    case Knight => 3
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   413
    case Pawn   => 1
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   414
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   415
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   416
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   417
// ... a tiny bit more useful: Roman Numerals
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   418
321
7b0055205ec9 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   419
sealed abstract class RomanDigit 
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   420
case object I extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   421
case object V extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   422
case object X extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   423
case object L extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   424
case object C extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   425
case object D extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   426
case object M extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   427
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   428
type RomanNumeral = List[RomanDigit] 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   429
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   430
List(X,I,M,A)
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   431
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   432
/*
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   433
I    -> 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   434
II   -> 2
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   435
III  -> 3
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   436
IV   -> 4
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   437
V    -> 5
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   438
VI   -> 6
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   439
VII  -> 7
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   440
VIII -> 8
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   441
IX   -> 9
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   442
X    -> 10
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   443
*/
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   444
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   445
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   446
  case Nil => 0
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   447
  case M::r    => 1000 + RomanNumeral2Int(r)  
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   448
  case C::M::r => 900 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   449
  case D::r    => 500 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   450
  case C::D::r => 400 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   451
  case C::r    => 100 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   452
  case X::C::r => 90 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   453
  case L::r    => 50 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   454
  case X::L::r => 40 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   455
  case X::r    => 10 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   456
  case I::X::r => 9 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   457
  case V::r    => 5 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   458
  case I::V::r => 4 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   459
  case I::r    => 1 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   460
}
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   461
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   462
RomanNumeral2Int(List(I,V))             // 4
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   463
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   464
RomanNumeral2Int(List(V,I))             // 6
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   465
RomanNumeral2Int(List(I,X))             // 9
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   466
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   467
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   468
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   469
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   470
abstract class Rexp
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   471
case object ZERO extends Rexp                      // matches nothing
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   472
case object ONE extends Rexp                       // matches the empty string
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   473
case class CHAR(c: Char) extends Rexp              // matches a character c
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   474
case class ALT(r1: Rexp, r2: Rexp) extends Rexp    // alternative
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   475
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp    // sequence
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   476
case class STAR(r: Rexp) extends Rexp              // star
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   477
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   478
def depth(r: Rexp) : Int = r match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   479
  case ZERO => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   480
  case ONE => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   481
  case CHAR(_) => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   482
  case ALT(r1, r2) => 1 + List(depth(r1), depth(r2)).max
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   483
  case SEQ(r1, r2) => 1 + List(depth(r1), depth(r2)).max
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   484
  case STAR(r1) => 1 + depth(r1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   485
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   486
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   487
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   488
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   489
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   490
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   491
// expressions (essentially trees)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   492
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   493
abstract class Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   494
case class N(n: Int) extends Exp                  // for numbers
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   495
case class Plus(e1: Exp, e2: Exp) extends Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   496
case class Times(e1: Exp, e2: Exp) extends Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   497
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   498
def string(e: Exp) : String = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   499
  case N(n) => s"$n"
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   500
  case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   501
  case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   502
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   503
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   504
val e = Plus(N(9), Times(N(3), N(4)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   505
e.toString
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   506
println(string(e))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   507
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   508
def eval(e: Exp) : Int = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   509
  case N(n) => n
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   510
  case Plus(e1, e2) => eval(e1) + eval(e2) 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   511
  case Times(e1, e2) => eval(e1) * eval(e2) 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   512
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   513
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   514
println(eval(e))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   515
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   516
// simplification rules:
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   517
// e + 0, 0 + e => e 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   518
// e * 0, 0 * e => 0
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   519
// e * 1, 1 * e => e
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   520
//
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   521
// (....9 ....)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   522
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   523
def simp(e: Exp) : Exp = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   524
  case N(n) => N(n)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   525
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   526
    case (N(0), e2s) => e2s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   527
    case (e1s, N(0)) => e1s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   528
    case (e1s, e2s) => Plus(e1s, e2s)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   529
  }  
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   530
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   531
    case (N(0), _) => N(0)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   532
    case (_, N(0)) => N(0)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   533
    case (N(1), e2s) => e2s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   534
    case (e1s, N(1)) => e1s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   535
    case (e1s, e2s) => Times(e1s, e2s)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   536
  }  
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   537
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   538
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   539
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   540
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   541
println(string(e2))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   542
println(string(simp(e2)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   543
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   544
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   545
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   546
// String interpolations as patterns
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   547
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   548
val date = "2019-11-26"
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   549
val s"$year-$month-$day" = date
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   550
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   551
def parse_date(date: String) : Option[(Int, Int, Int)]= date match {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   552
  case s"$year-$month-$day" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   553
  case s"$day/$month/$year" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   554
  case s"$day.$month.$year" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   555
  case _ => None
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   556
} 
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   557
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   558
parse_date("2019-11-26")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   559
parse_date("26/11/2019")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   560
parse_date("26.11.2019")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   561
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   562
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   563
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   564
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   565
// Map type (upper-case)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   566
//=======================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   567
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   568
// Note the difference between map and Map
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   569
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   570
val m = Map(1 -> "one", 2 -> "two", 10 -> "many")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   571
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   572
List((1, "one"), (2, "two"), (10, "many")).toMap
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   573
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   574
m.get(1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   575
m.get(4)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   576
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   577
m.getOrElse(1, "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   578
m.getOrElse(4, "")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   579
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   580
val new_m = m + (10 -> "ten")
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   581
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   582
new_m.get(10)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   583
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   584
val m2 = for ((k, v) <- m) yield (k, v.toUpperCase)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   585
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   586
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   587
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   588
// groupBy function on Maps
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   589
val lst = List("one", "two", "three", "four", "five")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   590
lst.groupBy(_.head)
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   591
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   592
lst.groupBy(_.length)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   593
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   594
lst.groupBy(_.length).get(3)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   595
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   596
val grps = lst.groupBy(_.length)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   597
grps.keySet
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   598
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   599
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   600
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   601
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   602
// Tail recursion
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   603
//================
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   604
375
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   605
def fact(n: BigInt): BigInt = 
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   606
  if (n == 0) 1 else n * fact(n - 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   607
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   608
fact(10)              //ok
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   609
fact(10000)           // produces a stackoverflow
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   610
375
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   611
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   612
def factT(n: BigInt, acc: BigInt): BigInt =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   613
  if (n == 0) acc else factT(n - 1, n * acc)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   614
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   615
factT(10, 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   616
println(factT(100000, 1))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   617
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   618
// there is a flag for ensuring a function is tail recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   619
import scala.annotation.tailrec
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   620
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   621
@tailrec
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   622
def factT(n: BigInt, acc: BigInt): BigInt =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   623
  if (n == 0) acc else factT(n - 1, n * acc)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   624
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   625
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   626
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   627
// for tail-recursive functions the Scala compiler
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   628
// generates loop-like code, which does not need
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   629
// to allocate stack-space in each recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   630
// call; Scala can do this only for tail-recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   631
// functions
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   632
375
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   633
def length(xs: List[Int]) : Int = xs match {
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   634
  case Nil => 0
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   635
  case _ :: tail => 1 + length(tail)
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   636
}
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   637
375
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   638
@tailrec
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   639
def lengthT(xs: List[Int], acc : Int) : Int = xs match {
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   640
  case Nil => acc
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   641
  case _ :: tail => lengthT(tail, 1 + acc)
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   642
}
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   643
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 374
diff changeset
   644
lengthT(List.fill(10000000)(1), 0)
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   645
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   646
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   647
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   648
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   649
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   650
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   651
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   652
// Aside: concurrency 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   653
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar 
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   654
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   655
for (n <- (1 to 10)) println(n)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   656
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   657
import scala.collection.parallel.CollectionConverters._
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   658
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   659
for (n <- (1 to 10).par) println(n)
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   660
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   661
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   662
// for measuring time
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   663
def time_needed[T](n: Int, code: => T) = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   664
  val start = System.nanoTime()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   665
  for (i <- (0 to n)) code
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   666
  val end = System.nanoTime()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   667
  (end - start) / 1.0e9
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   668
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   669
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   670
val list = (1L to 10_000_000L).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   671
time_needed(10, for (n <- list) yield n + 42)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   672
time_needed(10, for (n <- list.par) yield n + 42)
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   673
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   674
// ...but par does not make everything faster
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   675
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   676
list.sum
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   677
list.par.sum
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   678
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   679
time_needed(10, list.sum)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   680
time_needed(10, list.par.sum)
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   681
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   682
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   683
// Mutable vs Immutable
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   684
//======================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   685
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   686
// Remember:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   687
// - no vars, no ++i, no +=
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   688
// - no mutable data-structures (no Arrays, no ListBuffers)
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   689
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   690
// But what the heck....lets try to count to 1 Mio in parallel
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   691
// 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   692
// requires
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   693
// scala-cli --extra-jars scala- parallel-collections_3-1.0.4.jar
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   694
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   695
import scala.collection.parallel.CollectionConverters._
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   696
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   697
def test() = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   698
  var cnt = 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   699
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   700
  for(i <- (1 to 100_000).par) cnt += 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   701
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   702
  println(s"Should be 100000: $cnt")
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   703
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   704
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   705
test()
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   706
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   707
// Or
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   708
// Q: Count how many elements are in the intersections of 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   709
//    two sets?
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   710
// A; IMPROPER WAY (mutable counter)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   711
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   712
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   713
  var count = 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   714
  for (x <- A.par; if B contains x) count += 1 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   715
  count
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   716
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   717
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   718
val A = (0 to 999).toSet
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   719
val B = (0 to 999 by 4).toSet
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   720
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   721
count_intersection(A, B)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   722
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   723
// but do not try to add .par to the for-loop above
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   724
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   725
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   726
//propper parallel version
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   727
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   728
  A.par.count(x => B contains x)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   729
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 449
diff changeset
   730
count_intersection2(A, B)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   731
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   732