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