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