|      5 // |      5 // | 
|      6 // option type  |      6 // option type  | 
|      7 // higher-order function |      7 // higher-order function | 
|      8  |      8  | 
|      9  |      9  | 
|         |     10 def add(x: Int, y: Int) : Int = x + y | 
|         |     11  | 
|         |     12 def plus5(x: Int) : Int = add(5, x) | 
|         |     13  | 
|         |     14 plus5(6) | 
|         |     15  | 
|         |     16 def add2(x: Int)(y: Int) : Int = x + y | 
|         |     17  | 
|         |     18 def plus3(y: Int) : Int => Int = add2(3)(y) | 
|         |     19  | 
|         |     20 plus3(9) | 
|         |     21  | 
|         |     22 List(1,2,3,4,5).map(add2(3)) | 
|         |     23 List(1,2,3,4,5).map(add(3, _)) | 
|         |     24  | 
|         |     25 type Pos = (Int, Int) | 
|         |     26  | 
|         |     27 def test(p: Pos) = { | 
|         |     28   if (p._1 < 5 && p._2 < 5) { | 
|         |     29     Some(p) | 
|         |     30   } | 
|         |     31 } | 
|         |     32  | 
|         |     33 val l = List((1,2), (5,3), (2,5), (1,3)) | 
|         |     34  | 
|         |     35 l.map(test).flatten | 
|     10  |     36  | 
|     11 // Recursion Again ;o) |     37 // Recursion Again ;o) | 
|     12 //==================== |     38 //==================== | 
|     13  |     39  | 
|     14  |     40  | 
|     55 // some starting URLs for the crawler |     81 // some starting URLs for the crawler | 
|     56 val startURL = """https://nms.kcl.ac.uk/christian.urban/""" |     82 val startURL = """https://nms.kcl.ac.uk/christian.urban/""" | 
|     57  |     83  | 
|     58 crawl(startURL, 2) |     84 crawl(startURL, 2) | 
|     59  |     85  | 
|     60  |     86 for (x <- List(1,2,3,4,5,6)) println(x) | 
|     61  |     87  | 
|     62 // a primitive email harvester |     88 // a primitive email harvester | 
|     63 def emails(url: String, n: Int) : Set[String] = { |     89 def emails(url: String, n: Int) : Set[String] = { | 
|     64   if (n == 0) Set() |     90   if (n == 0) Set() | 
|     65   else { |     91   else { | 
|     66     println(s"  Visiting: $n $url") |     92     println(s"  Visiting: $n $url") | 
|     67     val page = get_page(url) |     93     val page = get_page(url) | 
|     68     val new_emails = email_pattern.findAllIn(page).toSet |     94     val new_emails = email_pattern.findAllIn(page).toSet | 
|     69     new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten |     95     new_emails ++ (for (u <- get_all_URLs(page).par) yield emails(u, n - 1)).flatten | 
|     70   } |     96   } | 
|     71 } |     97 } | 
|     72  |     98  | 
|     73 emails(startURL, 2) |     99 emails(startURL, 3) | 
|     74  |    100  | 
|     75  |    101  | 
|     76 // if we want to explore the internet "deeper", then we |    102 // if we want to explore the internet "deeper", then we | 
|     77 // first have to parallelise the request of webpages: |    103 // first have to parallelise the request of webpages: | 
|     78 // |    104 // | 
|    107 // the first n prefixes of xs |    133 // the first n prefixes of xs | 
|    108 // for 1 => include xs |    134 // for 1 => include xs | 
|    109  |    135  | 
|    110 def moves(xs: List[Int], n: Int) : List[List[Int]] = (xs, n) match { |    136 def moves(xs: List[Int], n: Int) : List[List[Int]] = (xs, n) match { | 
|    111   case (Nil, _) => Nil |    137   case (Nil, _) => Nil | 
|    112   case (xs, 0) => Nil |    138   case (_, 0) => Nil | 
|    113   case (x::xs, n) => (x::xs) :: moves(xs, n - 1) |    139   case (y::ys, n) => xs :: moves(ys, n - 1) | 
|    114 } |    140 } | 
|    115  |    141  | 
|    116  |    142  | 
|    117 moves(List(5,1,0), 1) |    143 moves(List(5,1,0), 1) | 
|    118 moves(List(5,1,0), 2) |    144 moves(List(5,1,0), 2) | 
|    179  |    205  | 
|    180  |    206  | 
|    181 // User-defined Datatypes |    207 // User-defined Datatypes | 
|    182 //======================== |    208 //======================== | 
|    183  |    209  | 
|         |    210 abstract class Tree | 
|         |    211 case class Leaf(x: Int) extends Tree | 
|         |    212 case class Node(s: String, left: Tree, right: Tree) extends Tree  | 
|         |    213  | 
|         |    214 List(Leaf(20), Node("foo", Leaf(1), Leaf(2))) | 
|    184  |    215  | 
|    185 sealed abstract class Colour |    216 sealed abstract class Colour | 
|    186 case object Red extends Colour  |    217 case object Red extends Colour  | 
|    187 case object Green extends Colour  |    218 case object Green extends Colour  | 
|    188 case object Blue extends Colour |    219 case object Blue extends Colour | 
|         |    220 case object Yellow extends Colour | 
|    189  |    221  | 
|    190  |    222  | 
|    191 def fav_colour(c: Colour) : Boolean = c match { |    223 def fav_colour(c: Colour) : Boolean = c match { | 
|    192   case Red   => false |         | 
|    193   case Green => true |    224   case Green => true | 
|    194   case Blue  => false  |    225   case _  => false  | 
|    195 } |    226 } | 
|    196  |    227  | 
|    197 fav_colour(Green) |    228 fav_colour(Green) | 
|    198  |    229  | 
|    199 // ... a tiny bit more useful: Roman Numerals |    230 // ... a tiny bit more useful: Roman Numerals | 
|    269 // User-defined Datatypes and Pattern Matching |    300 // User-defined Datatypes and Pattern Matching | 
|    270 //============================================= |    301 //============================================= | 
|    271  |    302  | 
|    272 // trees |    303 // trees | 
|    273  |    304  | 
|         |    305  | 
|         |    306  | 
|         |    307 // expressions | 
|         |    308  | 
|    274 sealed abstract class Exp |    309 sealed abstract class Exp | 
|    275 case class N(n: Int) extends Exp                  // for numbers |    310 case class N(n: Int) extends Exp                  // for numbers | 
|    276 case class Plus(e1: Exp, e2: Exp) extends Exp |    311 case class Plus(e1: Exp, e2: Exp) extends Exp | 
|    277 case class Times(e1: Exp, e2: Exp) extends Exp |    312 case class Times(e1: Exp, e2: Exp) extends Exp | 
|    278  |    313  |