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