progs/lecture1.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 12:37:58 +0000
changeset 473 ac79c2e534bd
parent 471 135bf034ac30
child 481 e03a0100ec46
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
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     4
// - List, Sets, Strings, ... 
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     5
// - Value assignments (val vs var)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     6
// - How to define functions? (What is returned?)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     7
// - If-Conditions
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     8
// - For-Comprehensions (guards, with/without yield)
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
     9
// - String-Interpolations
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    10
//
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    11
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    12
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    13
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
    14
// Value assignments
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    15
// (their names should be lower case)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    16
//====================================
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    17
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    18
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    19
val x = 42
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    20
val y = 3 + 4 
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    21
val z = x / y
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    22
val x = 70
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    23
println(z)
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    24
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    25
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    26
// (you cannot reassign values: z = 9 will give an error)
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    27
//var z = 9
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    28
//z = 10
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    29
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    30
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    31
// Hello World
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
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    34
// an example of a stand-alone Scala file
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    35
// (in the assignments you must submit a plain Scala script)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    36
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    37
object Hello extends App { 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    38
  println("hello world")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    39
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    40
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    41
// can then be called with
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    42
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    43
// $> scalac hello-world.scala
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    44
// $> scala Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    45
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    46
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    47
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    48
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    49
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    50
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    51
//=============
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
    52
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    53
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    54
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    55
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    56
// picking an element in a list
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    57
val lst = List(1, 2, 3, 1)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    58
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    59
lst(0)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    60
lst(2)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    61
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    62
// head and tail
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    63
lst.head
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    64
lst.tail
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    65
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    66
// some alterative syntax for lists
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    67
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    68
Nil     // empty list
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    69
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    70
1 :: 2 :: 3 :: Nil
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    71
List(1, 2, 3) ::: List(4, 5, 6)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    72
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    73
// also
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    74
List(1, 2, 3) ++ List(3, 6, 5)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    75
Set(1, 2, 3) ++ Set(3, 6, 5)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    76
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    77
// ranges
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    78
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    79
(1 to 10).toList
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    80
(1 to 10).toList.toString
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
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    83
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
    84
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    85
// Equality in Scala is structural
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    86
//=================================
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    87
val a = "Dave"
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    88
val b = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    89
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    90
if (a == b) println("Equal") else println("Unequal")
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    91
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    92
Set(1,2,3) == Set(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    93
List(1,2,3) == List(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    94
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    95
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    96
// this applies to "concrete" values...pretty much 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    97
// everything; but for example you cannot compare 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    98
// functions (later), and also not arrays
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    99
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   100
Array(1) == Array(1)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   101
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   102
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   103
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   104
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   105
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   106
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   107
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   108
val tst = "This is a " ++ "test" 
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   109
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   110
print(tst) 
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   111
println(tst) 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   112
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   113
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   114
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   115
println(lst.toString)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   116
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   117
println(lst.mkString)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   118
println(lst.mkString(","))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   119
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   120
// some methods take more than one argument
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   121
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   122
println(lst.mkString("{", ",", "}"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   124
// (in this case .mkString can take no, one, 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   125
// or three arguments...this has to do with
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   126
// default arguments)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   127
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   128
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   129
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   130
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   131
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   132
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   133
List(1,2,3,1).toSet
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   134
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   135
"hello".toList
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   136
"hello".toSet
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   137
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   138
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   140
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   141
1   // an Int
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   142
1L  // a Long
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   143
1F  // a Float
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   144
1D  // a Double
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   145
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   146
// useful methods for lists
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   147
//=========================
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   148
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   149
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   150
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   151
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   152
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   153
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   154
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   155
List(1,2,3,4).drop(2).sum
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   156
List(1,2,3,4,3).indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   157
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   158
"1,2,3,4,5".split(",").mkString("\n")
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   159
"1,2,3,4,5".split(",").toList
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   160
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   161
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   162
"abcdefg".startsWith("abc")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   163
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   164
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   165
// Types (see slide)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   166
//===================
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   167
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   168
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   169
 
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   170
 * base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   171
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   172
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   173
    String, Char
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   174
    Boolean...
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   175
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   176
 * compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   177
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   178
    List[Int]
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   179
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   180
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   181
    List[(BigInt, String)]
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   182
    Option[Int]
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   183
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   184
 * user-defined types (later)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   185
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   186
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   187
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   188
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   189
// you can make the type of a value explicit
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   190
val name = "bob"
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   191
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
   192
val name : String = "bob"
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   194
// type errors
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   195
math.sqrt("64")
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   196
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   197
// produces
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   198
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   199
// Type Mismatch Error:
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   200
//   Found   : ("64" : String)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   201
//   Required: Double
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   202
//
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   203
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   204
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   205
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   206
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   207
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   208
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   209
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   210
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   211
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   212
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   213
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   214
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   215
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   216
List(("one", 1), ("two", 2), ("three", 3))
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   217
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   218
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   219
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   220
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   221
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   222
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   223
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   224
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   225
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   226
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   227
def str(x: Int) : String = x.toString
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   228
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   229
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   230
incr(3)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   231
double(4)
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   232
square(6)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   233
str(3)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   234
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   235
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   236
// The general scheme for a function: you have to give a 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   237
// type to each argument and a return type of the function
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   238
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   239
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   240
//     ....
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   241
//  }
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   242
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   243
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   244
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   245
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   246
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   247
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   248
// - Scala used to not have a then-keyword
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   249
// - !!both if-else branches need to be present!!
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   250
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   251
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   252
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   253
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   254
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   255
// Scala 3 introduced if-then-else - maybe people 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   256
// desperately needed it 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   257
def fact(n: Int) : Int = 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   258
  if n == 0 then 1 else n * fact(n - 1)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   259
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   260
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   261
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   262
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   263
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   264
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   265
   ==     equals
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   266
   !=     not equals
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   267
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   268
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   269
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   270
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   271
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   272
def fib(n: Int) : Int = {
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   273
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   274
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   275
}
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   276
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   277
fib(9)
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   278
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   279
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   280
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   281
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   282
def gcd(a: Int, b: Int) : Int = {
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   283
  if (b == 0) a 
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   284
  else  gcd(b, a % b)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   285
}
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   286
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   287
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   288
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   289
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   290
def power(x: Int, n: Int) : Int =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   291
  if (n == 0) 1 else x * power(x, n - 1) 
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   292
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   293
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   294
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   295
// BTW: no returns!!
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   296
// "last" line (expression) in a function determines the 
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   297
// result
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   298
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   299
def average(xs: List[Int]) : Int = {
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   300
  if (xs.length == 0) 0 
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   301
  else xs.sum / xs.length
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   302
}
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   303
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   304
average(List(3,4,5))
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   305
average(List())
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   306
average(Nil)
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   307
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   308
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   309
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   310
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   311
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   312
val lst = (1 to 10).toList
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   313
for (n <- lst) yield n * n 
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   314
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   315
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   316
for (n <- lst) yield { 
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   317
  square(n) + double(n)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   318
}
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   319
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   320
for (n <- (1 to 10).toList; 
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   321
     m <- (1 to 5).toList) yield (n, m, n * m)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   322
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   323
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   324
// you can assign the result of a for-comprehension
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   325
// to a value
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   326
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   327
  for (n <- (1 to 10).toList; 
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   328
       m <- (1 to 10).toList) yield n * m
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   329
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   330
println(mult_table.mkString)
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   331
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   332
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   333
// for-comprehensions also work for other
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   334
// collections
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   335
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   336
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
   337
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   338
for (n <- (1 to 10)) yield {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   339
  n * n  
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   340
}
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   341
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   342
// with if-predicates / filters
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   343
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   344
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   345
     m <- (1 to 3).toList;
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   346
     if (n + m) % 2 == 0) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   347
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   348
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   349
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   350
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   351
val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   352
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   353
for ((m, n) <- lst) yield m + n 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   354
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   355
for (p <- lst) yield p._1 + p._2 
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   356
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   357
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   358
// general pattern of for-yield 
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   359
// (yield can be several lines)
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   360
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   361
for (pat <- ...) yield {
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   362
  // potentially complicated
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   363
  // calculation of a result
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   364
}
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   365
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   366
// For without yield
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   367
//===================
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   368
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   369
// with only a side-effect (no list is produced),
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   370
// has no "yield"
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   371
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   372
for (n <- (1 to 10).toList) println(n * n)
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   373
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   374
for (n <- (1 to 10).toList) yield n * n
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   375
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   376
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   377
// BTW: a roundabout way of printing out a list, say
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   378
val lst = ('a' to 'm').toList
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   379
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   380
for (i <- (0 until lst.length)) println(lst(i))
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   381
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   382
// ...why not just the following? Why making your life 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   383
// so complicated?
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   384
for (c <- lst) println(c)
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   385
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   386
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   387
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   388
// Functions producing multiple outputs
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   389
//======================================
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   390
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   391
def get_ascii(c: Char) : (Char, Int) = 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   392
  (c, c.toInt)
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   393
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   394
get_ascii('a')
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
// .maxBy, sortBy with pairs
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   398
def get_length(s: String) : (String, Int) = 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   399
  (s, s.length) 
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   400
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   401
val lst = List("zero", "one", "two", "three", "four", "ten")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   402
val strs = for (s <- lst) yield get_length(s)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   403
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   404
strs.sortBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   405
strs.sortBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   406
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   407
strs.maxBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   408
strs.maxBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   409
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   410
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   411
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   412
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   413
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   414
// Aside: concurrency 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   415
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar 
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   416
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   417
for (n <- (1 to 10)) println(n)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   418
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   419
import scala.collection.parallel.CollectionConverters._
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   420
32
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
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   432
val list = (1L to 10_000_000L).toList
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   433
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   434
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   435
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   436
// ...but par does not make everything faster
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   437
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   438
list.sum
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   439
list.par.sum
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   440
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   441
time_needed(10, list.sum)
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   442
time_needed(10, list.par.sum)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   443
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   444
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   445
// Mutable vs Immutable
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   446
//======================
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   447
//
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   448
// Remember:
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   449
// - no vars, no ++i, no +=
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   450
// - no mutable data-structures (no Arrays, no ListBuffers)
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   451
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   452
// But what the heck....lets try to count to 1 Mio in parallel
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 367
diff changeset
   453
import scala.collection.parallel.CollectionConverters._
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   454
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   455
var cnt = 0
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   456
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   457
for(i <- (1 to 100_000).par) cnt += 1
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   458
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   459
println(s"Should be 100000: $cnt")
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   460
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   461
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   462
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   463
// Or
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   464
// Q: Count how many elements are in the intersections of 
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   465
//    two sets?
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   466
// A; IMPROPER WAY (mutable counter)
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   467
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   468
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   469
  var count = 0
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   470
  for (x <- A.par; if B contains x) count += 1 
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   471
  count
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   472
}
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   473
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   474
val A = (0 to 999).toSet
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   475
val B = (0 to 999 by 4).toSet
200
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
count_intersection(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   478
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   479
// but do not try to add .par to the for-loop above
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   480
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   481
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   482
//propper parallel version
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   483
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   484
  A.par.count(x => B contains x)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   485
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   486
count_intersection2(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   487
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   488
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   489
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   490
// String Interpolations
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   491
//=======================
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   492
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   493
def cube(n: Int) : Int = n * n * n
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   494
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   495
val n = 3
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   496
println("The cube of " + n + " is " + cube(n) + ".")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   497
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   498
println(s"The cube of $n is ${cube(n)}.")
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   499
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   500
// or even
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   501
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   502
println(s"The cube of $n is ${n * n * n}.")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   503
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   504
// helpful for debugging purposes
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   505
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   506
//     "The most effective debugging tool is still careful 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   507
//          thought, coupled with judiciously placed print 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   508
//                                             statements."
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   509
//       — Brian W. Kernighan, in Unix for Beginners (1979)
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   510
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   511
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   512
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   513
def gcd_db(a: Int, b: Int) : Int = {
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   514
  println(s"Function called with $a and $b.")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   515
  if (b == 0) a else gcd_db(b, a % b)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   516
}
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   517
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   518
gcd_db(48, 18)
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   519
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   520
// you can also implement your own string interpolations
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   521
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   522
extension (sc: StringContext) {
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   523
    def i(args: Any*): String = s"\t${sc.s(args:_*)}\n"
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   524
    def l(args: Any*): String = s"${sc.s(args:_*)}:\n"
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   525
}
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   526
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   527
// this allows you to write things like
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   528
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   529
i"add ${3+2}" 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   530
l"some_fresh_name"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   531
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   532
// Further Information
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   533
//=====================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   534
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   535
// We are going to use Scala 3 and the scala-cli repl (easier to use)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   536
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   537
//  https://scala-cli.virtuslab.org
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   538
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   539
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   540
// The Scala homepage and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   541
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   542
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   543
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   544
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   545
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   546
// It should be fairly easy to install the scala-cli binary and
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   547
// run Scala on the commandline. People also use Scala with 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   548
// Vim and Jedit. I currently settled on Codium
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   549
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   550
//   https://code.visualstudio.com
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   551
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   552
// There are also plugins for Eclipse and IntelliJ - YMMV.
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   553
// Finally there are online editors specifically designed for 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   554
// running Scala applications (but do not blame me if you lose 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   555
// all what you typed in):
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   556
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   557
//   https://scalafiddle.io 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   558
//   https://scastie.scala-lang.org
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   559
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   560
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   561
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   562
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   563
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   564
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   565
//  https://dotty.epfl.ch/api/index.html
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   566
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   567
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   568
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   569
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   570
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   571
// There are also a massive number of Scala tutorials on youtube
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   572
// and there are tons of books and free material. Google is your 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   573
// friend. Just make sure you follow newer material about Scala 3.
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   574
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   575