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