| 51 |      1 | // Scala Lecture 2
 | 
|  |      2 | //=================
 | 
| 363 |      3 |  
 | 
| 468 |      4 | // - Options
 | 
|  |      5 | // - Higher-Order Functions (short-hand notation)
 | 
|  |      6 | // - maps (behind for-comprehensions)
 | 
|  |      7 | // - Pattern-Matching
 | 
|  |      8 | // - Recursion
 | 
| 361 |      9 | 
 | 
| 316 |     10 | // The Option Type
 | 
|  |     11 | //=================
 | 
|  |     12 | 
 | 
| 361 |     13 | // in Java, if something unusually happens, you return null 
 | 
|  |     14 | // or raise an exception
 | 
| 316 |     15 | //
 | 
|  |     16 | //in Scala you use Options instead
 | 
|  |     17 | //   - if the value is present, you use Some(value)
 | 
|  |     18 | //   - if no value is present, you use None
 | 
| 204 |     19 | 
 | 
|  |     20 | 
 | 
| 316 |     21 | List(7,2,3,4,5,6).find(_ < 4)
 | 
|  |     22 | List(5,6,7,8,9).find(_ < 4)
 | 
| 212 |     23 | 
 | 
| 361 |     24 | // Int:      ..., 0, 1, 2,...
 | 
|  |     25 | // Boolean:  true false
 | 
|  |     26 | //
 | 
|  |     27 | // List[Int]: Nil, List(_) 
 | 
|  |     28 | //
 | 
|  |     29 | // Option[Int]: None, Some(0), Some(1), ...
 | 
| 444 |     30 | // Option[Boolean]: None, Some(true), Some(false)
 | 
| 361 |     31 | // Option[...]: None, Some(_)
 | 
|  |     32 | 
 | 
|  |     33 | def safe_div(x: Int, y: Int) : Option[Int] = 
 | 
|  |     34 |   if (y == 0) None else Some(x / y)
 | 
|  |     35 | 
 | 
| 478 |     36 | safe_div(10 + 5, 0)  
 | 
| 361 |     37 | 
 | 
| 444 |     38 | List(1,2,3,4,5,6).indexOf(7)
 | 
| 468 |     39 | List[Int]().min
 | 
| 478 |     40 | List[Int](3,4,5).minOption
 | 
| 444 |     41 | 
 | 
| 361 |     42 | 
 | 
| 310 |     43 | 
 | 
| 316 |     44 | // better error handling with Options (no exceptions)
 | 
|  |     45 | //
 | 
|  |     46 | //  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
 | 
| 204 |     47 | //
 | 
| 316 |     48 | 
 | 
| 468 |     49 | import scala.util._      // Try,...
 | 
|  |     50 | import io.Source         // fromURL
 | 
| 316 |     51 | 
 | 
| 478 |     52 | val my_url = "https://nms.kcl.ac.uk/christian.urban2/"
 | 
| 316 |     53 | 
 | 
| 361 |     54 | Source.fromURL(my_url)("ISO-8859-1").mkString
 | 
| 444 |     55 | Source.fromURL(my_url)("ISO-8859-1").getLines().toList
 | 
| 316 |     56 | 
 | 
| 361 |     57 | Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("")
 | 
| 316 |     58 | 
 | 
| 361 |     59 | Try(Some(Source.fromURL(my_url)("ISO-8859-1").mkString)).getOrElse(None)
 | 
| 204 |     60 | 
 | 
|  |     61 | 
 | 
| 316 |     62 | // the same for files
 | 
| 444 |     63 | 
 | 
| 361 |     64 | Try(Some(Source.fromFile("test.txt")("ISO-8859-1").mkString)).getOrElse(None)
 | 
| 316 |     65 | 
 | 
| 444 |     66 | Try(Source.fromFile("test.txt")("ISO-8859-1").mkString).toOption
 | 
|  |     67 | 
 | 
|  |     68 | Using(Source.fromFile("test.txt")("ISO-8859-1"))(_.mkString).toOption
 | 
| 204 |     69 | 
 | 
| 319 |     70 | // how to implement a function for reading 
 | 
| 444 |     71 | // (lines) from files...
 | 
| 319 |     72 | //
 | 
| 316 |     73 | def get_contents(name: String) : List[String] = 
 | 
| 444 |     74 |   Source.fromFile(name)("ISO-8859-1").getLines().toList
 | 
| 204 |     75 | 
 | 
| 319 |     76 | get_contents("text.txt")
 | 
| 316 |     77 | get_contents("test.txt")
 | 
| 204 |     78 | 
 | 
| 316 |     79 | // slightly better - return Nil
 | 
|  |     80 | def get_contents(name: String) : List[String] = 
 | 
| 361 |     81 |   Try(Source.fromFile(name)("ISO-8859-1").getLines.toList).getOrElse(List())
 | 
| 204 |     82 | 
 | 
| 316 |     83 | get_contents("text.txt")
 | 
| 204 |     84 | 
 | 
| 316 |     85 | // much better - you record in the type that things can go wrong 
 | 
|  |     86 | def get_contents(name: String) : Option[List[String]] = 
 | 
| 444 |     87 |   Try(Some(Source.fromFile(name)("ISO-8859-1").getLines().toList)).getOrElse(None)
 | 
| 204 |     88 | 
 | 
| 316 |     89 | get_contents("text.txt")
 | 
|  |     90 | get_contents("test.txt")
 | 
| 204 |     91 | 
 | 
|  |     92 | 
 | 
| 317 |     93 | // operations on options
 | 
| 204 |     94 | 
 | 
| 317 |     95 | val lst = List(None, Some(1), Some(2), None, Some(3))
 | 
| 204 |     96 | 
 | 
| 317 |     97 | lst.flatten
 | 
| 204 |     98 | 
 | 
| 317 |     99 | Some(1).get
 | 
|  |    100 | None.get
 | 
| 310 |    101 | 
 | 
| 317 |    102 | Some(1).isDefined
 | 
|  |    103 | None.isDefined
 | 
| 310 |    104 | 
 | 
| 361 |    105 | for (x <- lst) yield x.getOrElse(0)
 | 
| 310 |    106 | 
 | 
| 361 |    107 | 
 | 
|  |    108 | 
 | 
|  |    109 | val ps = List((3, 0), (4, 2), (6, 2), 
 | 
|  |    110 |               (2, 0), (1, 0), (1, 1))
 | 
| 317 |    111 | 
 | 
|  |    112 | // division where possible
 | 
|  |    113 | 
 | 
|  |    114 | for ((x, y) <- ps) yield {
 | 
|  |    115 |   if (y == 0) None else Some(x / y)
 | 
|  |    116 | }
 | 
|  |    117 | 
 | 
| 361 |    118 | 
 | 
|  |    119 | 
 | 
| 317 |    120 | // getOrElse is for setting a default value
 | 
|  |    121 | 
 | 
|  |    122 | val lst = List(None, Some(1), Some(2), None, Some(3))
 | 
|  |    123 | 
 | 
| 361 |    124 | 
 | 
|  |    125 | // a function that turns strings into numbers 
 | 
|  |    126 | // (similar to .toInt)
 | 
|  |    127 | Integer.parseInt("1234")
 | 
| 318 |    128 | 
 | 
|  |    129 | 
 | 
|  |    130 | def get_me_an_int(s: String) : Option[Int] = 
 | 
|  |    131 |  Try(Some(Integer.parseInt(s))).getOrElse(None)
 | 
| 310 |    132 | 
 | 
|  |    133 | 
 | 
| 317 |    134 | // This may not look any better than working with null in Java, but to
 | 
|  |    135 | // see the value, you have to put yourself in the shoes of the
 | 
|  |    136 | // consumer of the get_me_an_int function, and imagine you didn't
 | 
|  |    137 | // write that function.
 | 
|  |    138 | //
 | 
|  |    139 | // In Java, if you didn't write this function, you'd have to depend on
 | 
|  |    140 | // the Javadoc of the get_me_an_int. If you didn't look at the Javadoc, 
 | 
| 318 |    141 | // you might not know that get_me_an_int could return null, and your 
 | 
| 317 |    142 | // code could potentially throw a NullPointerException.
 | 
| 310 |    143 | 
 | 
|  |    144 | 
 | 
| 317 |    145 | // even Scala is not immune to problems like this:
 | 
| 310 |    146 | 
 | 
| 317 |    147 | List(5,6,7,8,9).indexOf(7)
 | 
|  |    148 | List(5,6,7,8,9).indexOf(10)
 | 
|  |    149 | List(5,6,7,8,9)(-1)
 | 
| 310 |    150 | 
 | 
|  |    151 | 
 | 
| 320 |    152 | Try({
 | 
|  |    153 |   val x = 3
 | 
|  |    154 |   val y = 0
 | 
|  |    155 |   Some(x / y)
 | 
|  |    156 | }).getOrElse(None)
 | 
| 204 |    157 | 
 | 
| 323 |    158 | 
 | 
|  |    159 | // minOption 
 | 
|  |    160 | // maxOption 
 | 
|  |    161 | // minByOption 
 | 
|  |    162 | // maxByOption
 | 
|  |    163 | 
 | 
| 204 |    164 | // Higher-Order Functions
 | 
|  |    165 | //========================
 | 
|  |    166 | 
 | 
|  |    167 | // functions can take functions as arguments
 | 
| 319 |    168 | // and produce functions as result
 | 
| 204 |    169 | 
 | 
|  |    170 | def even(x: Int) : Boolean = x % 2 == 0
 | 
|  |    171 | def odd(x: Int) : Boolean = x % 2 == 1
 | 
|  |    172 | 
 | 
| 478 |    173 | def inc(x: Int) : Int = x + 1
 | 
| 204 |    174 | val lst = (1 to 10).toList
 | 
|  |    175 | 
 | 
| 478 |    176 | lst.filter(_ % 2 == 0)
 | 
| 320 |    177 | lst.count(odd)
 | 
| 212 |    178 | lst.find(even)
 | 
| 320 |    179 | lst.exists(even)
 | 
| 212 |    180 | 
 | 
| 362 |    181 | lst.find(_ < 4)
 | 
| 320 |    182 | lst.filter(_ < 4) 
 | 
| 362 |    183 | 
 | 
| 478 |    184 | val x = 3 < 4
 | 
|  |    185 | 
 | 
| 362 |    186 | def less4(x: Int) : Boolean = x < 4
 | 
|  |    187 | lst.find(less4)
 | 
| 478 |    188 | lst.find(x => !(x < 4))
 | 
| 362 |    189 | 
 | 
|  |    190 | lst.filter(x => x % 2 == 0)
 | 
| 318 |    191 | lst.filter(_ % 2 == 0)
 | 
| 204 |    192 | 
 | 
| 320 |    193 | 
 | 
| 362 |    194 | lst.sortWith((x, y) => x < y)
 | 
|  |    195 | lst.sortWith(_ > _)
 | 
| 204 |    196 | 
 | 
| 318 |    197 | // but this only works when the arguments are clear, but 
 | 
|  |    198 | // not with multiple occurences
 | 
|  |    199 | lst.find(n => odd(n) && n > 2)
 | 
|  |    200 | 
 | 
|  |    201 | 
 | 
| 444 |    202 | // lexicographic ordering
 | 
| 362 |    203 | val ps = List((3, 0), (3, 2), (4, 2), (2, 2), 
 | 
|  |    204 |               (2, 0), (1, 1), (1, 0))
 | 
| 318 |    205 | 
 | 
| 212 |    206 | def lex(x: (Int, Int), y: (Int, Int)) : Boolean = 
 | 
|  |    207 |   if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
 | 
|  |    208 | 
 | 
|  |    209 | ps.sortWith(lex)
 | 
| 204 |    210 | 
 | 
| 320 |    211 | ps.sortBy(x => x._1)
 | 
| 204 |    212 | ps.sortBy(_._2)
 | 
|  |    213 | 
 | 
|  |    214 | ps.maxBy(_._1)
 | 
|  |    215 | ps.maxBy(_._2)
 | 
|  |    216 | 
 | 
|  |    217 | 
 | 
| 212 |    218 | // maps (lower-case)
 | 
|  |    219 | //===================
 | 
| 204 |    220 | 
 | 
| 212 |    221 | def double(x: Int): Int = x + x
 | 
| 204 |    222 | def square(x: Int): Int = x * x
 | 
|  |    223 | 
 | 
|  |    224 | val lst = (1 to 10).toList
 | 
|  |    225 | 
 | 
| 362 |    226 | lst.map(square)
 | 
| 212 |    227 | lst.map(x => (double(x), square(x)))
 | 
|  |    228 | 
 | 
| 362 |    229 | // works also for strings
 | 
|  |    230 | def tweet(c: Char) = c.toUpper
 | 
|  |    231 | 
 | 
| 478 |    232 | "Hello\nWorld".map(tweet)
 | 
| 362 |    233 | 
 | 
|  |    234 | 
 | 
|  |    235 | // this can be iterated
 | 
|  |    236 | 
 | 
|  |    237 | lst.map(square).filter(_ > 4)
 | 
|  |    238 | 
 | 
| 363 |    239 | lst.map(square).find(_ > 4)
 | 
|  |    240 | lst.map(square).find(_ > 4).map(double)
 | 
|  |    241 | 
 | 
|  |    242 | lst.map(square)
 | 
| 362 |    243 |    .find(_ > 4)
 | 
| 363 |    244 |    .map(double)
 | 
|  |    245 | 
 | 
|  |    246 | 
 | 
|  |    247 | // Option Type and maps
 | 
|  |    248 | //======================
 | 
|  |    249 | 
 | 
|  |    250 | // a function that turns strings into numbers (similar to .toInt)
 | 
|  |    251 | Integer.parseInt("12u34")
 | 
|  |    252 | 
 | 
|  |    253 | // maps on Options
 | 
|  |    254 | 
 | 
|  |    255 | import scala.util._
 | 
| 362 |    256 | 
 | 
| 363 |    257 | def get_me_an_int(s: String) : Option[Int] = 
 | 
|  |    258 |  Try(Some(Integer.parseInt(s))).getOrElse(None)
 | 
|  |    259 | 
 | 
|  |    260 | get_me_an_int("12345").map(_ % 2 == 0)
 | 
|  |    261 | get_me_an_int("12u34").map(_ % 2 == 0)
 | 
|  |    262 | 
 | 
|  |    263 | 
 | 
|  |    264 | 
 | 
|  |    265 | val lst = List("12345", "foo", "5432", "bar", "x21", "456")
 | 
|  |    266 | for (x <- lst) yield get_me_an_int(x)
 | 
|  |    267 | 
 | 
|  |    268 | // summing up all the numbers
 | 
|  |    269 | 
 | 
|  |    270 | lst.map(get_me_an_int).flatten.sum
 | 
|  |    271 | 
 | 
|  |    272 | 
 | 
|  |    273 | 
 | 
| 204 |    274 | 
 | 
| 319 |    275 | // this is actually how for-comprehensions are
 | 
|  |    276 | // defined in Scala
 | 
| 204 |    277 | 
 | 
|  |    278 | lst.map(n => square(n))
 | 
|  |    279 | for (n <- lst) yield square(n)
 | 
|  |    280 | 
 | 
| 318 |    281 | // lets define our own higher-order functions
 | 
|  |    282 | // type of functions is for example Int => Int
 | 
| 204 |    283 | 
 | 
| 212 |    284 | 
 | 
| 363 |    285 | def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
 | 
|  |    286 | {
 | 
| 204 |    287 |   if (lst == Nil) Nil
 | 
|  |    288 |   else f(lst.head) :: my_map_int(lst.tail, f)
 | 
|  |    289 | }
 | 
|  |    290 | 
 | 
|  |    291 | my_map_int(lst, square)
 | 
|  |    292 | 
 | 
|  |    293 | // same function using pattern matching: a kind
 | 
|  |    294 | // of switch statement on steroids (see more later on)
 | 
|  |    295 | 
 | 
| 319 |    296 | def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
 | 
| 362 |    297 |   lst match {
 | 
|  |    298 |     case Nil => Nil
 | 
|  |    299 |     case x::xs => f(x)::my_map_int(xs, f)
 | 
|  |    300 |   }
 | 
| 204 |    301 | 
 | 
|  |    302 | 
 | 
| 363 |    303 | 
 | 
|  |    304 | val biglst = (1 to 10000).toList
 | 
|  |    305 | my_map_int(biglst, double)
 | 
|  |    306 | 
 | 
|  |    307 | (1 to 10000000).toList.map(double)
 | 
|  |    308 | 
 | 
| 204 |    309 | // other function types
 | 
|  |    310 | //
 | 
|  |    311 | // f1: (Int, Int) => Int
 | 
|  |    312 | // f2: List[String] => Option[Int]
 | 
|  |    313 | // ... 
 | 
|  |    314 | 
 | 
|  |    315 | 
 | 
| 320 |    316 | 
 | 
| 204 |    317 | 
 | 
|  |    318 | 
 | 
| 212 |    319 | // Map type (upper-case)
 | 
|  |    320 | //=======================
 | 
| 204 |    321 | 
 | 
|  |    322 | // Note the difference between map and Map
 | 
|  |    323 | 
 | 
| 364 |    324 | val m = Map(1 -> "one", 2 -> "two", 10 -> "many")
 | 
| 320 |    325 | 
 | 
| 364 |    326 | List((1, "one"), (2, "two"), (10, "many")).toMap
 | 
| 320 |    327 | 
 | 
| 364 |    328 | m.get(1)
 | 
|  |    329 | m.get(4)
 | 
| 204 |    330 | 
 | 
| 364 |    331 | m.getOrElse(1, "")
 | 
|  |    332 | m.getOrElse(4, "")
 | 
| 204 |    333 | 
 | 
| 364 |    334 | val new_m = m + (10 -> "ten")
 | 
| 204 |    335 | 
 | 
| 364 |    336 | new_m.get(10)
 | 
|  |    337 | 
 | 
|  |    338 | val m2 = for ((k, v) <- m) yield (k, v.toUpperCase)
 | 
| 204 |    339 | 
 | 
|  |    340 | 
 | 
| 318 |    341 | 
 | 
| 319 |    342 | // groupBy function on Maps
 | 
| 364 |    343 | val lst = List("one", "two", "three", "four", "five")
 | 
|  |    344 | lst.groupBy(_.head)
 | 
| 204 |    345 | 
 | 
| 364 |    346 | lst.groupBy(_.length)
 | 
| 204 |    347 | 
 | 
| 364 |    348 | lst.groupBy(_.length).get(3)
 | 
|  |    349 | 
 | 
|  |    350 | val grps = lst.groupBy(_.length)
 | 
|  |    351 | grps.keySet
 | 
| 204 |    352 | 
 | 
| 478 |    353 | // naive quicksort with "On" function
 | 
|  |    354 | 
 | 
|  |    355 | def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = {
 | 
|  |    356 |   if (xs.size < 2) xs
 | 
|  |    357 |   else {
 | 
|  |    358 |    val pivot = xs.head
 | 
|  |    359 |    val (left, right) = xs.partition(f(_) < f(pivot))
 | 
|  |    360 |    sortOn(f, left) ::: pivot :: sortOn(f, right.tail)
 | 
|  |    361 |   }
 | 
|  |    362 | } 
 | 
|  |    363 | 
 | 
|  |    364 | sortOn(identity, List(99,99,99,98,10,-3,2)) 
 | 
|  |    365 | sortOn(n => - n, List(99,99,99,98,10,-3,2))
 | 
| 204 |    366 | 
 | 
| 51 |    367 | 
 | 
| 192 |    368 | 
 | 
|  |    369 | // Pattern Matching
 | 
|  |    370 | //==================
 | 
|  |    371 | 
 | 
| 468 |    372 | // A powerful tool which has even landed in Java during 
 | 
|  |    373 | // the last few years (https://inside.java/2021/06/13/podcast-017/).
 | 
|  |    374 | // ...Scala already has it for many years and the concept is
 | 
|  |    375 | // older than your friendly lecturer, that is stone old  ;o)
 | 
| 192 |    376 | 
 | 
|  |    377 | // The general schema:
 | 
|  |    378 | //
 | 
|  |    379 | //    expression match {
 | 
|  |    380 | //       case pattern1 => expression1
 | 
|  |    381 | //       case pattern2 => expression2
 | 
|  |    382 | //       ...
 | 
|  |    383 | //       case patternN => expressionN
 | 
|  |    384 | //    }
 | 
|  |    385 | 
 | 
|  |    386 | 
 | 
| 319 |    387 | // recall
 | 
| 365 |    388 | def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
 | 
|  |    389 |   lst match {
 | 
|  |    390 |     case Nil => Nil
 | 
|  |    391 |     case x::xs => f(x)::my_map_int(xs, f)
 | 
|  |    392 |   }
 | 
| 58 |    393 | 
 | 
| 468 |    394 | def my_map_option(opt: Option[Int], f: Int => Int) : Option[Int] = 
 | 
|  |    395 |   opt match {
 | 
| 365 |    396 |     case None => None
 | 
|  |    397 |     case Some(x) => Some(f(x))
 | 
|  |    398 |   }
 | 
| 58 |    399 | 
 | 
| 365 |    400 | my_map_option(None, x => x * x)
 | 
|  |    401 | my_map_option(Some(8), x => x * x)
 | 
| 192 |    402 | 
 | 
| 212 |    403 | 
 | 
| 192 |    404 | // you can also have cases combined
 | 
| 266 |    405 | def season(month: String) : String = month match {
 | 
| 192 |    406 |   case "March" | "April" | "May" => "It's spring"
 | 
|  |    407 |   case "June" | "July" | "August" => "It's summer"
 | 
|  |    408 |   case "September" | "October" | "November" => "It's autumn"
 | 
| 204 |    409 |   case "December" => "It's winter"
 | 
|  |    410 |   case "January" | "February" => "It's unfortunately winter"
 | 
| 365 |    411 |   case _ => "Wrong month"
 | 
| 266 |    412 | }
 | 
|  |    413 | 
 | 
| 365 |    414 | // pattern-match on integers
 | 
|  |    415 | 
 | 
|  |    416 | def fib(n: Int) : Int = n match { 
 | 
|  |    417 |   case 0 | 1 => 1
 | 
|  |    418 |   case n => fib(n - 1) + fib(n - 2)
 | 
|  |    419 | }
 | 
|  |    420 | 
 | 
|  |    421 | fib(10)
 | 
| 266 |    422 | 
 | 
| 204 |    423 | // Silly: fizz buzz
 | 
| 192 |    424 | def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
 | 
|  |    425 |   case (0, 0) => "fizz buzz"
 | 
|  |    426 |   case (0, _) => "fizz"
 | 
|  |    427 |   case (_, 0) => "buzz"
 | 
|  |    428 |   case _ => n.toString  
 | 
|  |    429 | }
 | 
|  |    430 | 
 | 
| 365 |    431 | for (n <- 1 to 20) 
 | 
| 192 |    432 |  println(fizz_buzz(n))
 | 
|  |    433 | 
 | 
|  |    434 | 
 | 
| 365 |    435 | val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
 | 
|  |    436 | 
 | 
|  |    437 | def my_flatten(xs: List[Option[Int]]): List[Int] = 
 | 
|  |    438 |  xs match {
 | 
|  |    439 |    case Nil => Nil 
 | 
|  |    440 |    case None::rest => my_flatten(rest)
 | 
|  |    441 |    case Some(v)::rest => v :: my_flatten(rest)
 | 
|  |    442 |  }
 | 
|  |    443 | 
 | 
|  |    444 | my_flatten(List(None, Some(1), Some(2), None, Some(3)))
 | 
|  |    445 | 
 | 
|  |    446 | 
 | 
|  |    447 | 
 | 
|  |    448 |  
 | 
|  |    449 | 
 | 
|  |    450 | 
 | 
| 278 |    451 | 
 | 
|  |    452 | 
 | 
| 309 |    453 | // Recursion
 | 
|  |    454 | //===========
 | 
|  |    455 | 
 | 
|  |    456 | 
 | 
| 318 |    457 | /* Say you have characters a, b, c.
 | 
|  |    458 |    What are all the combinations of a certain length?
 | 
| 309 |    459 | 
 | 
| 318 |    460 |    All combinations of length 2:
 | 
|  |    461 |   
 | 
|  |    462 |      aa, ab, ac, ba, bb, bc, ca, cb, cc
 | 
|  |    463 | 
 | 
|  |    464 |    Combinations of length 3:
 | 
|  |    465 |    
 | 
|  |    466 |      aaa, baa, caa, and so on......
 | 
| 309 |    467 | */
 | 
|  |    468 | 
 | 
| 320 |    469 | def combs(cs: List[Char], n: Int) : List[String] = {
 | 
|  |    470 |   if (n == 0) List("")
 | 
|  |    471 |   else for (c <- cs; s <- combs(cs, n - 1)) yield s"$c$s"
 | 
|  |    472 | }
 | 
|  |    473 | 
 | 
|  |    474 | combs(List('a', 'b', 'c'), 3)
 | 
|  |    475 | 
 | 
|  |    476 | 
 | 
|  |    477 | 
 | 
| 318 |    478 | def combs(cs: List[Char], l: Int) : List[String] = {
 | 
| 309 |    479 |   if (l == 0) List("")
 | 
| 318 |    480 |   else for (c <- cs; s <- combs(cs, l - 1)) yield s"$c$s"
 | 
| 309 |    481 | }
 | 
|  |    482 | 
 | 
| 318 |    483 | combs("abc".toList, 2)
 | 
|  |    484 | 
 | 
|  |    485 | 
 | 
| 329 |    486 | // When writing recursive functions you have to
 | 
|  |    487 | // think about three points
 | 
|  |    488 | // 
 | 
|  |    489 | // - How to start with a recursive function
 | 
|  |    490 | // - How to communicate between recursive calls
 | 
|  |    491 | // - Exit conditions
 | 
|  |    492 | 
 | 
|  |    493 | 
 | 
| 147 |    494 | 
 | 
| 318 |    495 | // A Recursive Web Crawler / Email Harvester
 | 
|  |    496 | //===========================================
 | 
| 204 |    497 | //
 | 
| 212 |    498 | // the idea is to look for links using the
 | 
|  |    499 | // regular expression "https?://[^"]*" and for
 | 
|  |    500 | // email addresses using another regex.
 | 
| 204 |    501 | 
 | 
|  |    502 | import io.Source
 | 
|  |    503 | import scala.util._
 | 
|  |    504 | 
 | 
|  |    505 | // gets the first 10K of a web-page
 | 
|  |    506 | def get_page(url: String) : String = {
 | 
|  |    507 |   Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
 | 
|  |    508 |     getOrElse { println(s"  Problem with: $url"); ""}
 | 
| 147 |    509 | }
 | 
|  |    510 | 
 | 
| 204 |    511 | // regex for URLs and emails
 | 
|  |    512 | val http_pattern = """"https?://[^"]*"""".r
 | 
|  |    513 | val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
 | 
|  |    514 | 
 | 
| 268 |    515 | //test case:
 | 
| 212 |    516 | //email_pattern.findAllIn
 | 
|  |    517 | //  ("foo bla christian@kcl.ac.uk 1234567").toList
 | 
|  |    518 | 
 | 
| 204 |    519 | 
 | 
|  |    520 | // drops the first and last character from a string
 | 
|  |    521 | def unquote(s: String) = s.drop(1).dropRight(1)
 | 
|  |    522 | 
 | 
|  |    523 | def get_all_URLs(page: String): Set[String] = 
 | 
|  |    524 |   http_pattern.findAllIn(page).map(unquote).toSet
 | 
|  |    525 | 
 | 
|  |    526 | // naive version of crawl - searches until a given depth,
 | 
|  |    527 | // visits pages potentially more than once
 | 
| 318 |    528 | def crawl(url: String, n: Int) : Unit = {
 | 
|  |    529 |   if (n == 0) ()
 | 
| 204 |    530 |   else {
 | 
|  |    531 |     println(s"  Visiting: $n $url")
 | 
| 318 |    532 |     for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
 | 
| 204 |    533 |   }
 | 
| 147 |    534 | }
 | 
|  |    535 | 
 | 
| 204 |    536 | // some starting URLs for the crawler
 | 
|  |    537 | val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
 | 
| 147 |    538 | 
 | 
| 204 |    539 | crawl(startURL, 2)
 | 
|  |    540 | 
 | 
|  |    541 | 
 | 
| 318 |    542 | // a primitive email harvester
 | 
|  |    543 | def emails(url: String, n: Int) : Set[String] = {
 | 
|  |    544 |   if (n == 0) Set()
 | 
|  |    545 |   else {
 | 
|  |    546 |     println(s"  Visiting: $n $url")
 | 
|  |    547 |     val page = get_page(url)
 | 
|  |    548 |     val new_emails = email_pattern.findAllIn(page).toSet
 | 
|  |    549 |     new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten
 | 
|  |    550 |   }
 | 
|  |    551 | }
 | 
| 55 |    552 | 
 | 
| 318 |    553 | emails(startURL, 3)
 | 
| 55 |    554 | 
 | 
|  |    555 | 
 | 
| 318 |    556 | // if we want to explore the internet "deeper", then we
 | 
|  |    557 | // first have to parallelise the request of webpages:
 | 
|  |    558 | //
 | 
|  |    559 | // scala -cp scala-parallel-collections_2.13-0.2.0.jar 
 | 
|  |    560 | // import scala.collection.parallel.CollectionConverters._
 | 
| 55 |    561 | 
 | 
| 53 |    562 | 
 | 
|  |    563 | 
 | 
|  |    564 | 
 | 
| 192 |    565 | 
 | 
| 319 |    566 | // Jumping Towers
 | 
|  |    567 | //================
 | 
| 278 |    568 | 
 | 
| 319 |    569 | 
 | 
| 364 |    570 | def moves(xs: List[Int], n: Int) : List[List[Int]] = 
 | 
|  |    571 |  (xs, n) match {
 | 
|  |    572 |    case (Nil, _) => Nil
 | 
| 366 |    573 |    case (_, 0) => Nil
 | 
| 364 |    574 |    case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
 | 
|  |    575 |  }
 | 
| 319 |    576 | 
 | 
| 366 |    577 | // List(5,5,1,0) -> moves(List(5,1,0), 5)
 | 
| 319 |    578 | moves(List(5,1,0), 1)
 | 
|  |    579 | moves(List(5,1,0), 2)
 | 
|  |    580 | moves(List(5,1,0), 5)
 | 
|  |    581 | 
 | 
|  |    582 | // checks whether a jump tour exists at all
 | 
|  |    583 | 
 | 
|  |    584 | def search(xs: List[Int]) : Boolean = xs match {
 | 
|  |    585 |   case Nil => true
 | 
| 366 |    586 |   case x::xs =>
 | 
|  |    587 |     if (xs.length < x) true 
 | 
|  |    588 |     else moves(xs, x).exists(search(_))
 | 
| 319 |    589 | }
 | 
|  |    590 | 
 | 
|  |    591 | 
 | 
|  |    592 | search(List(5,3,2,5,1,1))
 | 
|  |    593 | search(List(3,5,1,0,0,0,1))
 | 
|  |    594 | search(List(3,5,1,0,0,0,0,1))
 | 
|  |    595 | search(List(3,5,1,0,0,0,1,1))
 | 
|  |    596 | search(List(3,5,1))
 | 
|  |    597 | search(List(5,1,1))
 | 
|  |    598 | search(Nil)
 | 
|  |    599 | search(List(1))
 | 
|  |    600 | search(List(5,1,1))
 | 
|  |    601 | search(List(3,5,1,0,0,0,0,0,0,0,0,1))
 | 
|  |    602 | 
 | 
| 366 |    603 | 
 | 
|  |    604 | import scala.util._
 | 
|  |    605 | List.fill(100)(Random.nextInt(2))
 | 
|  |    606 | search(List.fill(100)(Random.nextInt(10)))
 | 
|  |    607 | 
 | 
| 319 |    608 | // generate *all* jump tours
 | 
|  |    609 | //    if we are only interested in the shortes one, we could
 | 
|  |    610 | //    shortcircut the calculation and only return List(x) in
 | 
|  |    611 | //    case where xs.length < x, because no tour can be shorter
 | 
|  |    612 | //    than 1
 | 
|  |    613 | // 
 | 
|  |    614 | 
 | 
|  |    615 | def jumps(xs: List[Int]) : List[List[Int]] = xs match {
 | 
|  |    616 |   case Nil => Nil
 | 
| 366 |    617 |   case x::xs => {
 | 
| 319 |    618 |     val children = moves(xs, x)
 | 
| 366 |    619 |     val results = 
 | 
|  |    620 |       children.map(cs => jumps(cs).map(x :: _)).flatten
 | 
| 319 |    621 |     if (xs.length < x) List(x) :: results else results
 | 
|  |    622 |   }
 | 
|  |    623 | }
 | 
|  |    624 | 
 | 
|  |    625 | jumps(List(3,5,1,2,1,2,1))
 | 
|  |    626 | jumps(List(3,5,1,2,3,4,1))
 | 
|  |    627 | jumps(List(3,5,1,0,0,0,1))
 | 
|  |    628 | jumps(List(3,5,1))
 | 
|  |    629 | jumps(List(5,1,1))
 | 
|  |    630 | jumps(Nil)
 | 
|  |    631 | jumps(List(1))
 | 
|  |    632 | jumps(List(5,1,2))
 | 
|  |    633 | moves(List(1,2), 5)
 | 
|  |    634 | jumps(List(1,5,1,2))
 | 
|  |    635 | jumps(List(3,5,1,0,0,0,0,0,0,0,0,1))
 | 
|  |    636 | 
 | 
|  |    637 | jumps(List(5,3,2,5,1,1)).minBy(_.length)
 | 
|  |    638 | jumps(List(1,3,5,8,9,2,6,7,6,8,9)).minBy(_.length)
 | 
|  |    639 | jumps(List(1,3,6,1,0,9)).minBy(_.length)
 | 
|  |    640 | jumps(List(2,3,1,1,2,4,2,0,1,1)).minBy(_.length)
 | 
|  |    641 | 
 | 
|  |    642 | 
 |