| 51 |      1 | // Scala Lecture 1
 | 
|  |      2 | //=================
 | 
| 14 |      3 | 
 | 
| 26 |      4 | // Value assignments
 | 
| 123 |      5 | // (their names should be lower case)
 | 
| 199 |      6 | //====================================
 | 
| 21 |      7 | 
 | 
| 202 |      8 | 
 | 
| 14 |      9 | val x = 42
 | 
|  |     10 | val y = 3 + 4
 | 
| 123 |     11 | val z = x / y
 | 
|  |     12 | 
 | 
|  |     13 | // (you cannot reassign values: z = 9 will give an error)
 | 
| 14 |     14 | 
 | 
|  |     15 | 
 | 
| 125 |     16 | // Hello World
 | 
|  |     17 | //=============
 | 
|  |     18 | 
 | 
| 199 |     19 | // an example of a stand-alone Scala file
 | 
|  |     20 | // (in the assignments you must submit a plain Scala script)
 | 
| 125 |     21 | 
 | 
|  |     22 | object Hello extends App { 
 | 
|  |     23 |   println("hello world")
 | 
|  |     24 | }
 | 
|  |     25 | 
 | 
| 199 |     26 | // can then be called with
 | 
| 125 |     27 | //
 | 
|  |     28 | // $> scalac hello-world.scala
 | 
|  |     29 | // $> scala Hello
 | 
|  |     30 | //
 | 
|  |     31 | // $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
 | 
|  |     32 | 
 | 
|  |     33 | 
 | 
|  |     34 | 
 | 
| 25 |     35 | // Collections
 | 
|  |     36 | //=============
 | 
| 14 |     37 | List(1,2,3,1)
 | 
|  |     38 | Set(1,2,3,1)
 | 
|  |     39 | 
 | 
|  |     40 | 1 to 10
 | 
|  |     41 | (1 to 10).toList
 | 
|  |     42 | 
 | 
|  |     43 | (1 until 10).toList
 | 
|  |     44 | 
 | 
| 18 |     45 | // an element in a list
 | 
| 33 |     46 | val lst = List(1, 2, 3, 1)
 | 
|  |     47 | lst(0)
 | 
|  |     48 | lst(2)
 | 
| 18 |     49 | 
 | 
| 34 |     50 | // some alterative syntax for lists
 | 
|  |     51 | 
 | 
| 189 |     52 | 1 :: 2 :: 3 :: Nil
 | 
| 23 |     53 | List(1, 2, 3) ::: List(4, 5, 6)
 | 
| 14 |     54 | 
 | 
| 199 |     55 | // Equality is structural
 | 
|  |     56 | //========================
 | 
|  |     57 | val a = "Dave"
 | 
|  |     58 | val b = "Dave"
 | 
|  |     59 | 
 | 
| 202 |     60 | if (a == b) println("Equal") else println("Unequal")
 | 
| 199 |     61 | 
 | 
|  |     62 | Set(1,2,3) == Set(3,1,2)
 | 
|  |     63 | List(1,2,3) == List(3,1,2)
 | 
|  |     64 | 
 | 
| 202 |     65 | val n1 = 3 + 7
 | 
|  |     66 | val n2 = 5 + 5
 | 
|  |     67 | 
 | 
|  |     68 | n1 == n2
 | 
| 199 |     69 | 
 | 
| 200 |     70 | // this applies to "concrete" values;
 | 
| 199 |     71 | // you cannot compare functions
 | 
|  |     72 | 
 | 
|  |     73 | 
 | 
| 25 |     74 | // Printing/Strings
 | 
|  |     75 | //==================
 | 
| 14 |     76 | 
 | 
|  |     77 | println("test")
 | 
| 15 |     78 | 
 | 
| 33 |     79 | val tst = "This is a " + "test\n" 
 | 
| 14 |     80 | println(tst)
 | 
|  |     81 | 
 | 
|  |     82 | val lst = List(1,2,3,1)
 | 
|  |     83 | 
 | 
| 202 |     84 | 
 | 
| 14 |     85 | println(lst.toString)
 | 
| 202 |     86 | println(lst.mkString(","))
 | 
| 14 |     87 | 
 | 
| 33 |     88 | println(lst.mkString(", "))
 | 
|  |     89 | 
 | 
| 14 |     90 | // some methods take more than one argument
 | 
| 202 |     91 | println(lst.mkString("{", ",", "}"))
 | 
| 14 |     92 | 
 | 
| 32 |     93 | 
 | 
| 200 |     94 | 
 | 
| 25 |     95 | // Conversion methods
 | 
|  |     96 | //====================
 | 
| 14 |     97 | 
 | 
|  |     98 | List(1,2,3,1).toString
 | 
|  |     99 | List(1,2,3,1).toSet
 | 
| 202 |    100 | "hello".toList.tail
 | 
| 14 |    101 | 1.toDouble
 | 
|  |    102 | 
 | 
| 25 |    103 | 
 | 
| 32 |    104 | // useful list methods
 | 
|  |    105 | 
 | 
|  |    106 | List(1,2,3,4).length
 | 
| 25 |    107 | List(1,2,3,4).reverse
 | 
| 32 |    108 | List(1,2,3,4).max
 | 
|  |    109 | List(1,2,3,4).min
 | 
|  |    110 | List(1,2,3,4).sum
 | 
|  |    111 | List(1,2,3,4).take(2).sum
 | 
|  |    112 | List(1,2,3,4).drop(2).sum
 | 
| 199 |    113 | List(1,2,3,4,3).indexOf(3)
 | 
| 32 |    114 | 
 | 
| 36 |    115 | "1,2,3,4,5".split(",").mkString("\n")
 | 
| 202 |    116 | "1,2,3,4,5".split(",").toList
 | 
| 36 |    117 | "1,2,3,4,5".split(",3,").mkString("\n")
 | 
| 25 |    118 | 
 | 
| 200 |    119 | "abcdefg".startsWith("abc")
 | 
|  |    120 | 
 | 
|  |    121 | 
 | 
| 140 |    122 | // Types (slide)
 | 
| 200 |    123 | //===============
 | 
| 25 |    124 | 
 | 
|  |    125 | /* Scala is a strongly typed language
 | 
|  |    126 |  
 | 
| 34 |    127 |  * some base types
 | 
| 14 |    128 | 
 | 
| 25 |    129 |     Int, Long, BigInt, Float, Double
 | 
|  |    130 |     String, Char
 | 
|  |    131 |     Boolean
 | 
|  |    132 | 
 | 
| 34 |    133 |  * some compound types 
 | 
| 12 |    134 | 
 | 
| 25 |    135 |     List[Int],
 | 
|  |    136 |     Set[Double]
 | 
|  |    137 |     Pairs: (Int, String)        
 | 
|  |    138 |     List[(BigInt, String)]
 | 
| 200 |    139 |     Option[Int]
 | 
| 25 |    140 | */
 | 
| 12 |    141 | 
 | 
| 23 |    142 | 
 | 
| 247 |    143 | val name: String = "leo"
 | 
|  |    144 | 
 | 
| 14 |    145 | 
 | 
| 265 |    146 | // type errors
 | 
|  |    147 | math.sqrt("64")
 | 
|  |    148 | 
 | 
|  |    149 | // produces
 | 
|  |    150 | //
 | 
|  |    151 | // error: type mismatch;
 | 
|  |    152 | // found   : String("64")
 | 
|  |    153 | // required: Double
 | 
|  |    154 | // math.sqrt("64")
 | 
|  |    155 | 
 | 
| 25 |    156 | // Pairs/Tuples
 | 
|  |    157 | //==============
 | 
| 14 |    158 | 
 | 
|  |    159 | val p = (1, "one")
 | 
|  |    160 | p._1
 | 
|  |    161 | p._2
 | 
|  |    162 | 
 | 
|  |    163 | val t = (4,1,2,3)
 | 
|  |    164 | t._4
 | 
|  |    165 | 
 | 
| 25 |    166 | 
 | 
| 200 |    167 | List(("one", 1), ("two", 2), ("three", 3))
 | 
|  |    168 | 
 | 
| 25 |    169 | // Function Definitions
 | 
|  |    170 | //======================
 | 
| 14 |    171 | 
 | 
| 123 |    172 | def incr(x: Int) : Int = x + 1
 | 
|  |    173 | def double(x: Int) : Int = x + x
 | 
|  |    174 | def square(x: Int) : Int = x * x
 | 
| 14 |    175 | 
 | 
| 202 |    176 | def str(x: Int) : String = x.toString
 | 
| 25 |    177 | square(6)
 | 
| 21 |    178 | 
 | 
|  |    179 | 
 | 
| 36 |    180 | // The general scheme for a function: you have to give a type 
 | 
|  |    181 | // to each argument and a return type of the function
 | 
|  |    182 | //
 | 
|  |    183 | //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
 | 
|  |    184 | //    body 
 | 
|  |    185 | //  }
 | 
|  |    186 | 
 | 
|  |    187 | 
 | 
| 200 |    188 | //
 | 
|  |    189 | // BTW: no returns!!
 | 
|  |    190 | // "last" line (expression) in a function determines the result
 | 
|  |    191 | //
 | 
|  |    192 | 
 | 
|  |    193 | def silly(n: Int) : Int = {
 | 
| 202 |    194 |   if (n < 10) n * n
 | 
|  |    195 |   else n + n
 | 
| 200 |    196 | }
 | 
|  |    197 | 
 | 
| 36 |    198 | 
 | 
| 123 |    199 | // If-Conditionals
 | 
|  |    200 | //=================
 | 
| 14 |    201 | 
 | 
| 200 |    202 | // - Scala does not have a then-keyword
 | 
|  |    203 | // - both if-else branches need to be present
 | 
| 189 |    204 | 
 | 
| 143 |    205 | def fact(n: Int) : Int = 
 | 
| 14 |    206 |   if (n == 0) 1 else n * fact(n - 1)
 | 
|  |    207 | 
 | 
| 36 |    208 | 
 | 
|  |    209 | fact(5)
 | 
|  |    210 | fact(150)
 | 
|  |    211 | 
 | 
| 25 |    212 | /* boolean operators
 | 
|  |    213 |  
 | 
|  |    214 |    ==     equals
 | 
|  |    215 |    !      not
 | 
|  |    216 |    && ||  and, or
 | 
|  |    217 | */
 | 
| 15 |    218 | 
 | 
|  |    219 | 
 | 
| 189 |    220 | def fact2(n: BigInt) : BigInt = 
 | 
| 14 |    221 |   if (n == 0) 1 else n * fact2(n - 1)
 | 
|  |    222 | 
 | 
| 25 |    223 | fact2(150)
 | 
|  |    224 | 
 | 
| 26 |    225 | 
 | 
| 189 |    226 | def fib(n: Int) : Int =
 | 
| 14 |    227 |   if (n == 0) 1 else
 | 
| 26 |    228 |     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
 | 
| 14 |    229 | 
 | 
|  |    230 | 
 | 
| 26 |    231 | //gcd - Euclid's algorithm
 | 
|  |    232 | 
 | 
| 202 |    233 | def gcd(a: Int, b: Int) : Int = {
 | 
|  |    234 |   if (b == 0) a 
 | 
|  |    235 |   else {
 | 
|  |    236 |     val foo = 42
 | 
|  |    237 |     gcd(b, a % b)
 | 
|  |    238 |   }  
 | 
|  |    239 | }
 | 
| 26 |    240 | 
 | 
|  |    241 | gcd(48, 18)
 | 
|  |    242 | 
 | 
| 14 |    243 | 
 | 
| 123 |    244 | def power(x: Int, n: Int) : Int =
 | 
| 199 |    245 |   if (n == 0) 1 else x * power(x, n - 1) 
 | 
| 123 |    246 | 
 | 
|  |    247 | power(5, 5)
 | 
|  |    248 | 
 | 
|  |    249 | 
 | 
| 199 |    250 | // Option type
 | 
|  |    251 | //=============
 | 
|  |    252 | 
 | 
|  |    253 | //in Java if something unusually happens, you return null
 | 
|  |    254 | //
 | 
| 200 |    255 | //in Scala you use Options instead
 | 
| 199 |    256 | //   - if the value is present, you use Some(value)
 | 
|  |    257 | //   - if no value is present, you use None
 | 
|  |    258 | 
 | 
|  |    259 | 
 | 
|  |    260 | List(7,2,3,4,5,6).find(_ < 4)
 | 
|  |    261 | List(5,6,7,8,9).find(_ < 4)
 | 
|  |    262 | 
 | 
|  |    263 | 
 | 
|  |    264 | 
 | 
|  |    265 | // error handling with Options (no exceptions)
 | 
|  |    266 | //
 | 
|  |    267 | //  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
 | 
|  |    268 | //
 | 
|  |    269 | import scala.util._
 | 
|  |    270 | import io.Source
 | 
|  |    271 | 
 | 
| 202 |    272 | val my_url = "https://nms.imperial.ac.uk/christian.urban/"
 | 
| 199 |    273 | 
 | 
|  |    274 | Source.fromURL(my_url).mkString
 | 
|  |    275 | 
 | 
|  |    276 | Try(Source.fromURL(my_url).mkString).getOrElse("")
 | 
|  |    277 | 
 | 
|  |    278 | Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
 | 
|  |    279 | 
 | 
|  |    280 | 
 | 
|  |    281 | // the same for files
 | 
| 202 |    282 | Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
 | 
| 199 |    283 | 
 | 
| 200 |    284 | // function reading something from files...
 | 
|  |    285 | 
 | 
|  |    286 | def get_contents(name: String) : List[String] = 
 | 
|  |    287 |   Source.fromFile(name).getLines.toList
 | 
|  |    288 | 
 | 
|  |    289 | get_contents("test.txt")
 | 
|  |    290 | 
 | 
|  |    291 | // slightly better - return Nil
 | 
|  |    292 | def get_contents(name: String) : List[String] = 
 | 
| 202 |    293 |   Try(Source.fromFile(name).getLines.toList).getOrElse(List())
 | 
| 200 |    294 | 
 | 
|  |    295 | get_contents("text.txt")
 | 
|  |    296 | 
 | 
|  |    297 | // much better - you record in the type that things can go wrong 
 | 
|  |    298 | def get_contents(name: String) : Option[List[String]] = 
 | 
|  |    299 |   Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
 | 
|  |    300 | 
 | 
|  |    301 | get_contents("text.txt")
 | 
|  |    302 | 
 | 
|  |    303 | 
 | 
| 199 |    304 | 
 | 
| 25 |    305 | // String Interpolations
 | 
|  |    306 | //=======================
 | 
| 14 |    307 | 
 | 
| 26 |    308 | val n = 3
 | 
|  |    309 | println("The square of " + n + " is " + square(n) + ".")
 | 
|  |    310 | 
 | 
|  |    311 | println(s"The square of ${n} is ${square(n)}.")
 | 
|  |    312 | 
 | 
|  |    313 | 
 | 
| 200 |    314 | // helpful for debugging purposes
 | 
|  |    315 | //
 | 
|  |    316 | //         "The most effective debugging tool is still careful thought, 
 | 
|  |    317 | //          coupled with judiciously placed print statements."
 | 
|  |    318 | //                   — Brian W. Kernighan, in Unix for Beginners (1979)
 | 
|  |    319 | 
 | 
| 26 |    320 | 
 | 
| 123 |    321 | def gcd_db(a: Int, b: Int) : Int = {
 | 
| 26 |    322 |   println(s"Function called with ${a} and ${b}.")
 | 
|  |    323 |   if (b == 0) a else gcd_db(b, a % b)
 | 
|  |    324 | }
 | 
|  |    325 | 
 | 
|  |    326 | gcd_db(48, 18)
 | 
|  |    327 | 
 | 
| 14 |    328 | 
 | 
| 124 |    329 | // Asserts/Testing
 | 
| 200 |    330 | //=================
 | 
| 14 |    331 | 
 | 
| 32 |    332 | assert(gcd(48, 18) == 6)
 | 
|  |    333 | 
 | 
|  |    334 | assert(gcd(48, 18) == 5, "The gcd test failed")
 | 
|  |    335 | 
 | 
|  |    336 | 
 | 
| 26 |    337 | // For-Comprehensions (not For-Loops)
 | 
|  |    338 | //====================================
 | 
| 14 |    339 | 
 | 
| 202 |    340 | 
 | 
|  |    341 | for (n <- (1 to 10).toList) yield { 
 | 
|  |    342 |   square(n) + 1
 | 
|  |    343 | }
 | 
| 14 |    344 | 
 | 
| 25 |    345 | for (n <- (1 to 10).toList; 
 | 
|  |    346 |      m <- (1 to 10).toList) yield m * n
 | 
| 21 |    347 | 
 | 
|  |    348 | 
 | 
| 26 |    349 | val mult_table = 
 | 
|  |    350 |   for (n <- (1 to 10).toList; 
 | 
|  |    351 |        m <- (1 to 10).toList) yield m * n
 | 
|  |    352 | 
 | 
| 202 |    353 | println(mult_table.mkString)
 | 
| 26 |    354 | mult_table.sliding(10,10).mkString("\n")
 | 
|  |    355 | 
 | 
| 200 |    356 | // the list/set/... can also be constructed in any 
 | 
|  |    357 | // other way
 | 
| 202 |    358 | for (n <- Set(10,12,4,5,7,8,10)) yield n * n
 | 
| 189 |    359 | 
 | 
| 25 |    360 | 
 | 
| 200 |    361 | // with if-predicates / filters
 | 
| 32 |    362 | 
 | 
|  |    363 | for (n <- (1 to 3).toList; 
 | 
|  |    364 |      m <- (1 to 3).toList;
 | 
| 202 |    365 |      if (n + m) % 2 == 0;
 | 
|  |    366 |      if (n * m) < 2) yield (n, m)
 | 
| 32 |    367 | 
 | 
| 148 |    368 | for (n <- (1 to 3).toList; 
 | 
|  |    369 |      m <- (1 to 3).toList;
 | 
| 202 |    370 |      if ((((n + m) % 2 == 0)))) yield (n, m)
 | 
| 32 |    371 | 
 | 
| 26 |    372 | // with patterns
 | 
|  |    373 | 
 | 
| 199 |    374 | val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
 | 
| 26 |    375 | 
 | 
| 199 |    376 | for ((m, n) <- lst) yield m + n 
 | 
|  |    377 | 
 | 
|  |    378 | for (p <- lst) yield p._1 + p._2 
 | 
| 26 |    379 | 
 | 
| 25 |    380 | 
 | 
| 200 |    381 | // general pattern of for-yield
 | 
| 189 |    382 | 
 | 
| 200 |    383 | for (p <- ...) yield {
 | 
| 189 |    384 |   // potentially complicated
 | 
|  |    385 |   // calculation of a result
 | 
|  |    386 | }
 | 
|  |    387 | 
 | 
| 200 |    388 | // Functions producing multiple outputs
 | 
|  |    389 | //======================================
 | 
| 189 |    390 | 
 | 
| 200 |    391 | def get_ascii(c: Char) : (Char, Int) = (c, c.toInt)
 | 
|  |    392 | 
 | 
|  |    393 | get_ascii('a')
 | 
|  |    394 | 
 | 
|  |    395 | 
 | 
|  |    396 | // .maxBy, sortBy with pairs
 | 
|  |    397 | def get_length(s: String) : (String, Int) = (s, s.length) 
 | 
|  |    398 | 
 | 
|  |    399 | val lst = List("zero", "one", "two", "three", "four", "ten")
 | 
|  |    400 | val strs = for (s <- lst) yield get_length(s)
 | 
|  |    401 | 
 | 
|  |    402 | strs.sortBy(_._2)
 | 
|  |    403 | strs.sortBy(_._1)
 | 
|  |    404 | 
 | 
|  |    405 | strs.maxBy(_._2)
 | 
|  |    406 | strs.maxBy(_._1)
 | 
|  |    407 | 
 | 
|  |    408 | 
 | 
|  |    409 | // For without yield
 | 
|  |    410 | //===================
 | 
| 25 |    411 | 
 | 
| 36 |    412 | // with only a side-effect (no list is produced),
 | 
| 32 |    413 | // has no "yield"
 | 
|  |    414 | 
 | 
|  |    415 | for (n <- (1 to 10)) println(n)
 | 
|  |    416 | 
 | 
|  |    417 | 
 | 
| 199 |    418 | // BTW: a roundabout way of printing out a list, say
 | 
|  |    419 | val lst = ('a' to 'm').toList
 | 
| 140 |    420 | 
 | 
| 202 |    421 | for (n <- lst) println(n)
 | 
|  |    422 | 
 | 
| 199 |    423 | for (i <- (0 until lst.length)) println(lst(i))
 | 
|  |    424 | 
 | 
| 200 |    425 | // Why not just? Why making your life so complicated?
 | 
| 199 |    426 | for (c <- lst) println(c)
 | 
|  |    427 | 
 | 
|  |    428 | // Aside: concurrency 
 | 
|  |    429 | // (ONLY WORKS OUT-OF-THE-BOX IN SCALA 2.11.8, not in SCALA 2.12)
 | 
|  |    430 | // (would need to have this wrapped into a function, or
 | 
|  |    431 | //  REPL called with scala -Yrepl-class-based)
 | 
| 32 |    432 | for (n <- (1 to 10)) println(n)
 | 
|  |    433 | for (n <- (1 to 10).par) println(n)
 | 
|  |    434 | 
 | 
|  |    435 | 
 | 
| 36 |    436 | // for measuring time
 | 
| 140 |    437 | def time_needed[T](n: Int, code: => T) = {
 | 
| 32 |    438 |   val start = System.nanoTime()
 | 
| 140 |    439 |   for (i <- (0 to n)) code
 | 
| 32 |    440 |   val end = System.nanoTime()
 | 
| 140 |    441 |   (end - start) / 1.0e9
 | 
| 32 |    442 | }
 | 
|  |    443 | 
 | 
| 140 |    444 | 
 | 
| 32 |    445 | val list = (1 to 1000000).toList
 | 
|  |    446 | time_needed(10, for (n <- list) yield n + 42)
 | 
|  |    447 | time_needed(10, for (n <- list.par) yield n + 42)
 | 
|  |    448 | 
 | 
|  |    449 | 
 | 
| 140 |    450 | 
 | 
| 200 |    451 | // Just for "Fun": Mutable vs Immutable
 | 
|  |    452 | //=======================================
 | 
|  |    453 | //
 | 
|  |    454 | // - no vars, no ++i, no +=
 | 
|  |    455 | // - no mutable data-structures (no Arrays, no ListBuffers)
 | 
| 137 |    456 | 
 | 
| 32 |    457 | 
 | 
| 200 |    458 | // Q: Count how many elements are in the intersections of two sets?
 | 
|  |    459 | 
 | 
|  |    460 | def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
 | 
|  |    461 |   var count = 0
 | 
|  |    462 |   for (x <- A; if (B contains x)) count += 1 
 | 
|  |    463 |   count
 | 
|  |    464 | }
 | 
| 32 |    465 | 
 | 
| 200 |    466 | val A = (1 to 1000).toSet
 | 
|  |    467 | val B = (1 to 1000 by 4).toSet
 | 
|  |    468 | 
 | 
|  |    469 | count_intersection(A, B)
 | 
|  |    470 | 
 | 
|  |    471 | // but do not try to add .par to the for-loop above
 | 
|  |    472 | 
 | 
| 32 |    473 | 
 | 
| 200 |    474 | //propper parallel version
 | 
|  |    475 | def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
 | 
|  |    476 |   A.par.count(x => B contains x)
 | 
|  |    477 | 
 | 
|  |    478 | count_intersection2(A, B)
 | 
|  |    479 | 
 | 
| 32 |    480 | 
 | 
| 265 |    481 | //another example
 | 
|  |    482 | def test() = {
 | 
|  |    483 |   var cnt = 0
 | 
|  |    484 |   for(i <- (1 to 1000000).par) cnt += 1
 | 
|  |    485 |   println(cnt)
 | 
|  |    486 | }
 | 
|  |    487 | 
 | 
|  |    488 | test()
 | 
|  |    489 | 
 | 
| 200 |    490 | //for measuring time
 | 
|  |    491 | def time_needed[T](n: Int, code: => T) = {
 | 
|  |    492 |   val start = System.nanoTime()
 | 
|  |    493 |   for (i <- (0 to n)) code
 | 
|  |    494 |   val end = System.nanoTime()
 | 
|  |    495 |   (end - start) / 1.0e9
 | 
|  |    496 | }
 | 
|  |    497 | 
 | 
|  |    498 | val A = (1 to 1000000).toSet
 | 
|  |    499 | val B = (1 to 1000000 by 4).toSet
 | 
|  |    500 | 
 | 
|  |    501 | time_needed(10, count_intersection(A, B))
 | 
|  |    502 | time_needed(10, count_intersection2(A, B))
 | 
|  |    503 | 
 | 
| 32 |    504 | 
 | 
|  |    505 | // Further Information
 | 
|  |    506 | //=====================
 | 
|  |    507 | 
 | 
| 200 |    508 | // The Scala homepage and general information is at
 | 
| 32 |    509 | //
 | 
|  |    510 | //  http://www.scala-lang.org
 | 
| 123 |    511 | //	http://docs.scala-lang.org
 | 
|  |    512 | //
 | 
|  |    513 | //
 | 
|  |    514 | // It should be fairly easy to install the Scala binary and
 | 
| 200 |    515 | // run Scala on the commandline. People also use Scala with 
 | 
|  |    516 | // Vim and Jedit. I currently settled on VS Code
 | 
| 123 |    517 | //
 | 
| 200 |    518 | //   https://code.visualstudio.com
 | 
| 123 |    519 | //
 | 
| 200 |    520 | // There are also plugins for Eclipse and IntelliJ - YMMV.
 | 
|  |    521 | // Finally there are online editors specifically designed for 
 | 
|  |    522 | // running Scala applications (but do not blame me if you lose 
 | 
|  |    523 | // all what you typed in):
 | 
| 123 |    524 | //
 | 
| 200 |    525 | //   https://scalafiddle.io 
 | 
|  |    526 | //   https://scastie.scala-lang.org
 | 
| 124 |    527 | //
 | 
| 123 |    528 | //
 | 
|  |    529 | //
 | 
|  |    530 | // Scala Library Docs
 | 
| 124 |    531 | //====================
 | 
| 123 |    532 | //
 | 
|  |    533 | //  http://www.scala-lang.org/api/current/
 | 
|  |    534 | //
 | 
|  |    535 | // Scala Tutorials
 | 
|  |    536 | //
 | 
|  |    537 | //  http://docs.scala-lang.org/tutorials/
 | 
|  |    538 | //
 | 
|  |    539 | // There are also a massive number of Scala tutorials on youtube
 | 
| 200 |    540 | // and there are tons of books and free material. Google is your 
 | 
|  |    541 | // friend.
 | 
| 32 |    542 | 
 | 
|  |    543 | 
 | 
| 170 |    544 | 
 | 
|  |    545 | 
 | 
|  |    546 | 
 | 
|  |    547 | 
 | 
|  |    548 | 
 | 
|  |    549 | 
 | 
|  |    550 | 
 | 
| 195 |    551 | 
 | 
|  |    552 | 
 | 
|  |    553 | 
 | 
|  |    554 | 
 | 
|  |    555 | 
 | 
|  |    556 | 
 |