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