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