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