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