progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 15 Nov 2018 03:35:38 +0000
changeset 202 f7bcb27d1940
parent 200 01ee4b576eb2
child 247 50a3b874008a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 36
diff changeset
     1
// Scala Lecture 1
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 36
diff changeset
     2
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     3
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
     4
// Value assignments
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     5
// (their names should be lower case)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
     6
//====================================
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
     7
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
     8
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     9
val x = 42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    10
val y = 3 + 4
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    11
val z = x / y
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    12
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    13
// (you cannot reassign values: z = 9 will give an error)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    15
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    16
// Hello World
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    17
//=============
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    18
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    19
// an example of a stand-alone Scala file
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    20
// (in the assignments you must submit a plain Scala script)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    21
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    22
object Hello extends App { 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    23
  println("hello world")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    24
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    25
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    26
// can then be called with
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    27
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    28
// $> scalac hello-world.scala
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    29
// $> scala Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    30
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    31
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    32
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    33
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    34
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    35
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    36
//=============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    37
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    40
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    44
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    45
// an element in a list
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    46
val lst = List(1, 2, 3, 1)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    47
lst(0)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    48
lst(2)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    49
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    50
// some alterative syntax for lists
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    51
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    52
1 :: 2 :: 3 :: Nil
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    53
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    54
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    55
// Equality is structural
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    56
//========================
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    57
val a = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    58
val b = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    59
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    60
if (a == b) println("Equal") else println("Unequal")
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    61
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    62
Set(1,2,3) == Set(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    63
List(1,2,3) == List(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    64
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    65
val n1 = 3 + 7
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    66
val n2 = 5 + 5
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    67
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    68
n1 == n2
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    69
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    70
// this applies to "concrete" values;
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    71
// you cannot compare functions
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    72
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    73
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    74
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    75
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    76
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    77
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    78
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    79
val tst = "This is a " + "test\n" 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    80
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    81
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    82
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    83
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    84
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    85
println(lst.toString)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    86
println(lst.mkString(","))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    87
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    88
println(lst.mkString(", "))
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    89
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    90
// some methods take more than one argument
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    91
println(lst.mkString("{", ",", "}"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    92
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    93
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    94
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    95
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    96
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    97
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    98
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    99
List(1,2,3,1).toSet
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   100
"hello".toList.tail
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   101
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   102
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   103
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   104
// useful list methods
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   105
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   106
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   107
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   108
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   109
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   110
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   111
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   112
List(1,2,3,4).drop(2).sum
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   113
List(1,2,3,4,3).indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   114
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   115
"1,2,3,4,5".split(",").mkString("\n")
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   116
"1,2,3,4,5".split(",").toList
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   117
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   118
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   119
"abcdefg".startsWith("abc")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   120
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   121
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   122
// Types (slide)
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   123
//===============
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   124
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   125
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   126
 
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   127
 * some base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   128
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   129
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   130
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   131
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   132
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   133
 * some compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   134
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   135
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   136
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   137
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   138
    List[(BigInt, String)]
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   139
    Option[Int]
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   140
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   141
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   142
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   143
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   144
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   145
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   146
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   147
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   148
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   149
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   150
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   151
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   152
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   153
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   154
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   155
List(("one", 1), ("two", 2), ("three", 3))
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   156
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   157
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   158
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   159
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   160
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   161
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   162
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   163
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   164
def str(x: Int) : String = x.toString
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   165
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   166
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   167
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   168
// The general scheme for a function: you have to give a type 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   169
// to each argument and a return type of the function
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   170
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   171
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   172
//    body 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   173
//  }
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   174
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   175
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   176
//
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   177
// BTW: no returns!!
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   178
// "last" line (expression) in a function determines the result
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   179
//
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   180
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   181
def silly(n: Int) : Int = {
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   182
  if (n < 10) n * n
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   183
  else n + n
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   184
}
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   185
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   186
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   187
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   188
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   189
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   190
// - Scala does not have a then-keyword
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   191
// - both if-else branches need to be present
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   192
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   193
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   194
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   195
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   196
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   197
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   198
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   199
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   200
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   201
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   202
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   203
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   204
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   205
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   206
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   207
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   208
def fact2(n: BigInt) : BigInt = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   209
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   210
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   211
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   212
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   213
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   214
def fib(n: Int) : Int =
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   215
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   216
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   217
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   218
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   219
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   220
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   221
def gcd(a: Int, b: Int) : Int = {
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   222
  if (b == 0) a 
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   223
  else {
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   224
    val foo = 42
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   225
    gcd(b, a % b)
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   226
  }  
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   227
}
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   228
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   229
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   230
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   231
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   232
def power(x: Int, n: Int) : Int =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   233
  if (n == 0) 1 else x * power(x, n - 1) 
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   234
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   235
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   236
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   237
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   238
// Option type
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   239
//=============
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   240
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   241
//in Java if something unusually happens, you return null
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   242
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   243
//in Scala you use Options instead
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   244
//   - if the value is present, you use Some(value)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   245
//   - if no value is present, you use None
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   246
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   247
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   248
List(7,2,3,4,5,6).find(_ < 4)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   249
List(5,6,7,8,9).find(_ < 4)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   250
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   251
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   252
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   253
// error handling with Options (no exceptions)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   254
//
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   255
//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   256
//
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   257
import scala.util._
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   258
import io.Source
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   259
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   260
val my_url = "https://nms.imperial.ac.uk/christian.urban/"
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   261
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   262
Source.fromURL(my_url).mkString
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   263
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   264
Try(Source.fromURL(my_url).mkString).getOrElse("")
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   265
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   266
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   267
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   268
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   269
// the same for files
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   270
Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   271
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   272
// function reading something from files...
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   273
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   274
def get_contents(name: String) : List[String] = 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   275
  Source.fromFile(name).getLines.toList
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   276
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   277
get_contents("test.txt")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   278
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   279
// slightly better - return Nil
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   280
def get_contents(name: String) : List[String] = 
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   281
  Try(Source.fromFile(name).getLines.toList).getOrElse(List())
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   282
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   283
get_contents("text.txt")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   284
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   285
// much better - you record in the type that things can go wrong 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   286
def get_contents(name: String) : Option[List[String]] = 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   287
  Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   288
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   289
get_contents("text.txt")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   290
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   291
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   292
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   293
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   294
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   295
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   296
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   297
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   298
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   299
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   300
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   301
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   302
// helpful for debugging purposes
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   303
//
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   304
//         "The most effective debugging tool is still careful thought, 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   305
//          coupled with judiciously placed print statements."
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   306
//                   — Brian W. Kernighan, in Unix for Beginners (1979)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   307
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   308
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   309
def gcd_db(a: Int, b: Int) : Int = {
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   310
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   311
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   312
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   313
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   314
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   315
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   316
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   317
// Asserts/Testing
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   318
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   319
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   320
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   321
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   322
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   323
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   324
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   325
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   326
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   327
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   328
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   329
for (n <- (1 to 10).toList) yield { 
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   330
  square(n) + 1
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   331
}
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   332
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   333
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   334
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   335
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   336
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   337
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   338
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   339
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   340
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   341
println(mult_table.mkString)
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   342
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   343
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   344
// the list/set/... can also be constructed in any 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   345
// other way
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   346
for (n <- Set(10,12,4,5,7,8,10)) yield n * n
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   347
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   348
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   349
// with if-predicates / filters
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   350
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   351
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   352
     m <- (1 to 3).toList;
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   353
     if (n + m) % 2 == 0;
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   354
     if (n * m) < 2) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   355
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   356
for (n <- (1 to 3).toList; 
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   357
     m <- (1 to 3).toList;
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   358
     if ((((n + m) % 2 == 0)))) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   359
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   360
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   361
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   362
val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   363
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   364
for ((m, n) <- lst) yield m + n 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   365
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   366
for (p <- lst) yield p._1 + p._2 
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   367
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   368
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   369
// general pattern of for-yield
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   370
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   371
for (p <- ...) yield {
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   372
  // potentially complicated
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   373
  // calculation of a result
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   374
}
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   375
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   376
// Functions producing multiple outputs
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   377
//======================================
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   378
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   379
def get_ascii(c: Char) : (Char, Int) = (c, c.toInt)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   380
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   381
get_ascii('a')
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   382
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   383
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   384
// .maxBy, sortBy with pairs
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   385
def get_length(s: String) : (String, Int) = (s, s.length) 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   386
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   387
val lst = List("zero", "one", "two", "three", "four", "ten")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   388
val strs = for (s <- lst) yield get_length(s)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   389
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   390
strs.sortBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   391
strs.sortBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   392
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   393
strs.maxBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   394
strs.maxBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   395
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   396
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   397
// For without yield
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   398
//===================
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   399
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   400
// with only a side-effect (no list is produced),
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   401
// has no "yield"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   402
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   403
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   404
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   405
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   406
// BTW: a roundabout way of printing out a list, say
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   407
val lst = ('a' to 'm').toList
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   408
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   409
for (n <- lst) println(n)
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   410
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   411
for (i <- (0 until lst.length)) println(lst(i))
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   412
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   413
// Why not just? Why making your life so complicated?
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   414
for (c <- lst) println(c)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   415
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   416
// Aside: concurrency 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   417
// (ONLY WORKS OUT-OF-THE-BOX IN SCALA 2.11.8, not in SCALA 2.12)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   418
// (would need to have this wrapped into a function, or
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   419
//  REPL called with scala -Yrepl-class-based)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   420
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   421
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   422
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   423
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   424
// for measuring time
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   425
def time_needed[T](n: Int, code: => T) = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   426
  val start = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   427
  for (i <- (0 to n)) code
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   428
  val end = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   429
  (end - start) / 1.0e9
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   430
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   431
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   432
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   433
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   434
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   435
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   436
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   437
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   438
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   439
// Just for "Fun": Mutable vs Immutable
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   440
//=======================================
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   441
//
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   442
// - no vars, no ++i, no +=
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   443
// - no mutable data-structures (no Arrays, no ListBuffers)
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   444
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   445
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   446
// Q: Count how many elements are in the intersections of two sets?
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   447
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   448
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   449
  var count = 0
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   450
  for (x <- A; if (B contains x)) count += 1 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   451
  count
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   452
}
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   453
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   454
val A = (1 to 1000).toSet
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   455
val B = (1 to 1000 by 4).toSet
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   456
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   457
count_intersection(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   458
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   459
// but do not try to add .par to the for-loop above
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   460
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   461
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   462
//propper parallel version
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   463
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   464
  A.par.count(x => B contains x)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   465
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   466
count_intersection2(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   467
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   468
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   469
//for measuring time
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   470
def time_needed[T](n: Int, code: => T) = {
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   471
  val start = System.nanoTime()
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   472
  for (i <- (0 to n)) code
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   473
  val end = System.nanoTime()
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   474
  (end - start) / 1.0e9
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   475
}
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   476
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   477
val A = (1 to 1000000).toSet
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   478
val B = (1 to 1000000 by 4).toSet
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   479
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   480
time_needed(10, count_intersection(A, B))
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   481
time_needed(10, count_intersection2(A, B))
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   482
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   483
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   484
// Further Information
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   485
//=====================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   486
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   487
// The Scala homepage and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   488
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   489
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   490
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   491
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   492
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   493
// It should be fairly easy to install the Scala binary and
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   494
// run Scala on the commandline. People also use Scala with 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   495
// Vim and Jedit. I currently settled on VS Code
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   496
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   497
//   https://code.visualstudio.com
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   498
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   499
// There are also plugins for Eclipse and IntelliJ - YMMV.
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   500
// Finally there are online editors specifically designed for 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   501
// running Scala applications (but do not blame me if you lose 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   502
// all what you typed in):
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   503
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   504
//   https://scalafiddle.io 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   505
//   https://scastie.scala-lang.org
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   506
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   507
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   508
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   509
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   510
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   511
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   512
//  http://www.scala-lang.org/api/current/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   513
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   514
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   515
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   516
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   517
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   518
// There are also a massive number of Scala tutorials on youtube
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   519
// and there are tons of books and free material. Google is your 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   520
// friend.
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   521
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   522
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   523
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   524
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   525
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   526
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   527
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   528
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   529
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   530
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   531
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   532
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   533
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   534
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   535