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