progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 08 Nov 2018 23:42:03 +0000
changeset 199 54befaf23648
parent 195 fc3ac7b70a06
child 200 01ee4b576eb2
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
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     8
val x = 42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     9
val y = 3 + 4
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    10
val z = x / y
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    11
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    12
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    13
// (you cannot reassign values: z = 9 will give an error)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    15
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    16
// Hello World
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    17
//=============
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    18
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    19
// an example of a stand-alone Scala file
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    20
// (in the assignments you must submit a plain Scala script)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    21
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    22
object Hello extends App { 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    23
  println("hello world")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    24
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    25
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    26
// can then be called with
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    27
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    28
// $> scalac hello-world.scala
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    29
// $> scala Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    30
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    31
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    32
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    33
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    34
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    35
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    36
//=============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    37
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    40
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    44
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    45
// an element in a list
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    46
val lst = List(1, 2, 3, 1)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    47
lst(0)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    48
lst(2)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    49
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    50
// some alterative syntax for lists
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    51
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    52
1 :: 2 :: 3 :: Nil
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    53
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    54
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    55
// Equality is structural
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    56
//========================
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    57
val a = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    58
val b = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    59
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    60
if (a == b) println("equal") else println("unequal")
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    61
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    62
Set(1,2,3) == Set(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    63
List(1,2,3) == List(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    64
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    65
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    66
// this applies for "concrete" values;
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    67
// you cannot compare functions
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    68
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    69
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    70
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    71
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    72
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    73
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    74
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    75
val tst = "This is a " + "test\n" 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    76
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    77
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    78
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    79
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    80
println(lst.toString)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    81
println(lst.mkString("\n"))
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    82
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    83
println(lst.mkString(", "))
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    84
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    85
// some methods take more than one argument
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    86
println(lst.mkString("[", ",", "]"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    87
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    88
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    89
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    90
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    91
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    92
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    93
List(1,2,3,1).toSet
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    94
"hello".toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    95
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    96
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    97
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    98
// useful list methods
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    99
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   100
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   101
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   102
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   103
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   104
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   105
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   106
List(1,2,3,4).drop(2).sum
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   107
List(1,2,3,4,3).indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   108
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   109
"1,2,3,4,5".split(",").mkString("\n")
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   110
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   111
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   112
// Types (slide)
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   113
//=======
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   114
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   115
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   116
 
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   117
 * some base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   118
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   119
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   120
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   121
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   122
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   123
 * some compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   124
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   125
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   126
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   127
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   128
    List[(BigInt, String)]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   129
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   130
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   131
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   132
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   133
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   134
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   135
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   136
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   137
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   138
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   140
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   141
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   142
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   143
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   144
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   145
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   146
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   147
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   148
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   149
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   150
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   151
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   152
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   153
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   154
// The general scheme for a function: you have to give a type 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   155
// to each argument and a return type of the function
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   156
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   157
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   158
//    body 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   159
//  }
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   160
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   161
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   162
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   163
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   164
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   165
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   166
// Scala does not have a then-keyword
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   167
// both if-else branches need to be present
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   168
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   169
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   170
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   171
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   172
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   173
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   174
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   175
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   176
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   177
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   178
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   179
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   180
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   181
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   182
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   183
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   184
def fact2(n: BigInt) : BigInt = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   185
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   186
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   187
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   188
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   189
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   190
def fib(n: Int) : Int =
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   191
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   192
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   194
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   195
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   196
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   197
def gcd(a: Int, b: Int) : Int =
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   198
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   199
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   200
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   201
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   202
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   203
def power(x: Int, n: Int) : Int =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   204
  if (n == 0) 1 else x * power(x, n - 1) 
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   205
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   206
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   207
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   208
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   209
// Option type
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   210
//=============
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   211
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   212
//in Java if something unusually happens, you return null
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   213
//
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   214
//in Scala you use Option
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   215
//   - if the value is present, you use Some(value)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   216
//   - if no value is present, you use None
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   217
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   218
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   219
List(7,2,3,4,5,6).find(_ < 4)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   220
List(5,6,7,8,9).find(_ < 4)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   221
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   222
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   223
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   224
// error handling with Options (no exceptions)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   225
//
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   226
//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   227
//
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   228
import scala.util._
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   229
import io.Source
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   230
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   231
val my_url = "https://nms.kcl.ac.uk/christian.urban/"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   232
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   233
Source.fromURL(my_url).mkString
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   234
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   235
Try(Source.fromURL(my_url).mkString).getOrElse("")
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   236
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   237
Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   238
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   239
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   240
// the same for files
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   241
Source.fromFile("test.txt").mkString
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   242
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   243
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   244
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   245
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   246
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   247
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   248
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   249
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   250
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   251
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   252
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   253
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   254
def gcd_db(a: Int, b: Int) : Int = {
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   255
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   256
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   257
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   258
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   259
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   260
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   261
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   262
// Asserts/Testing
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   263
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   264
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   265
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   266
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   267
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   268
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   269
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   270
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   271
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   272
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   273
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   274
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   275
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   276
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   277
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   278
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   279
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   280
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   281
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   282
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   283
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   284
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   285
// the list can also be constructed in any other way
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   286
for (n <- List(10,12,4,5,7,8,10)) yield n * n
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   287
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   288
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   289
// with if-predicates
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   290
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   291
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   292
     m <- (1 to 3).toList;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   293
     if (n + m) % 2 == 0) yield (n, m)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   294
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   295
for (n <- (1 to 3).toList; 
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   296
     m <- (1 to 3).toList;
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   297
     if ((n + m) % 2 == 0)) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   298
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   299
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   300
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   301
val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   302
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   303
for ((m, n) <- lst) yield m + n 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   304
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   305
for (p <- lst) yield p._1 + p._2 
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   306
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   307
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   308
// general pattern
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   309
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   310
for (x <- ...) yield {
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   311
  // potentially complicated
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   312
  // calculation of a result
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   313
}
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   314
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   315
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   316
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   317
// with only a side-effect (no list is produced),
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   318
// has no "yield"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   319
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   320
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   321
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   322
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   323
// BTW: a roundabout way of printing out a list, say
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   324
val lst = ('a' to 'm').toList
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   325
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   326
for (i <- (0 until lst.length)) println(lst(i))
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   327
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   328
// why not?
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   329
for (c <- lst) println(c)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   330
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   331
// Aside: concurrency 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   332
// (ONLY WORKS OUT-OF-THE-BOX IN SCALA 2.11.8, not in SCALA 2.12)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   333
// (would need to have this wrapped into a function, or
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   334
//  REPL called with scala -Yrepl-class-based)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   335
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   336
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   337
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   338
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   339
// for measuring time
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   340
def time_needed[T](n: Int, code: => T) = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   341
  val start = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   342
  for (i <- (0 to n)) code
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   343
  val end = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   344
  (end - start) / 1.0e9
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   345
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   346
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   347
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   348
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   349
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   350
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   351
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   352
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   353
// Function producing multiple outputs
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   354
//=====================================
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   355
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   356
def get_ascii(c: Char) : (Char, Int) = (c, c.toInt)
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   357
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   358
get_ascii('a')
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   359
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   360
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   361
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   362
// .maxBy, sortBy with pairs
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   363
def get_length(s: String) : (String, Int) = (s, s.length) 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   364
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   365
val lst = List("zero", "one", "two", "three", "four", "ten")
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   366
val strs = for (s <- lst) yield get_length(s)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   367
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   368
strs.sortBy(_._2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   369
strs.sortBy(_._1)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   370
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   371
strs.maxBy(_._2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   372
strs.maxBy(_._1)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   373
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   374
// Further Information
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   375
//=====================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   376
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   377
// The Scala home page and general information is at
32
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
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   380
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   381
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   382
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   383
// It should be fairly easy to install the Scala binary and
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   384
// run Scala on the commandline. There are also at least 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   385
// four IDEs you can use with Scala:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   386
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   387
//  (0) Some general information about setting up IDEs
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   388
//	    with Scala support can be found at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   389
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   390
//         http://docs.scala-lang.org/getting-started.html 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   391
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   392
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   393
//  (1) Eclipse for Scala (one big bundle)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   394
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   395
//         http://scala-ide.org/download/sdk.html
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   396
//  
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   397
//  (2) IntelliJ (needs additional Plugins)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   398
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   399
//         https://www.jetbrains.com/idea/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   400
//		   http://docs.scala-lang.org/getting-started-intellij-track/getting-started-with-scala-in-intellij.html	  
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   401
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   402
//  (3) Sublime (not free, but unlimited trial period; 
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   403
//	    needs Scala and SublimeREPL plugin)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   404
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   405
//         https://www.sublimetext.com
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   406
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   407
//  (4) Emacs (old-fashioned, but reliable)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   408
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   409
//         https://www.gnu.org/software/emacs/
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   410
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   411
//      I use the old scala-tool support for Emacs distributed at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   412
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   413
//         https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   414
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   415
//      but there is also support for the newer Ensime Scala Mode
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   416
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   417
//         http://ensime.org/editors/emacs/scala-mode/   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   418
//   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   419
// There is also Scala support in the Atom editor, but my
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   420
// experience is mixed. People also use Scala with Vim and Jedit.
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   421
// Finally there is an online editor specifically designed for 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   422
// running Scala applications (but do not blame mne if you lose all 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   423
// what you typed in):
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   424
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   425
//      https://scalafiddle.io 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   426
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   427
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   428
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   429
// All of the IDEs above support a REPL for Scala. Some of them have
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   430
// the very nifty feature of a Scala Worksheet -- you just save your
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   431
// file and it will be automatically evaluated and the result pasted
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   432
// into your file. However, this way of writing Scala code never worked
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   433
// for me. I just use the REPL.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   434
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   435
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   436
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   437
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   438
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   439
//  http://www.scala-lang.org/api/current/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   440
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   441
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   442
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   443
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   444
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   445
// There are also a massive number of Scala tutorials on youtube
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   446
// and there are tons of books and free material.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   447
//
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   448
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   449
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   450
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   451
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   452
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   453
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   454
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   455
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   456
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   457
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   458
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   459
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   460
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   461
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   462