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