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