progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 10 Jul 2018 15:09:28 +0100
changeset 189 ff815ca0bbcf
parent 170 37b1bfcdba79
child 195 fc3ac7b70a06
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)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
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
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    19
// an example of a stand-alone scala file;
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    20
// in the coursework students must submit 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    21
// plain scala "work-sheets"
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    22
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    23
object Hello extends App { 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    24
  println("hello world")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    25
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    26
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    27
// can be called with
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    28
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    29
// $> scalac hello-world.scala
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    30
// $> scala Hello
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    31
//
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    32
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
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
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    35
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    36
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    37
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    38
//=============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    40
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    44
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    45
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    46
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    47
// an element in a list
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    48
val lst = List(1, 2, 3, 1)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    49
lst(0)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    50
lst(2)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    51
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    52
// some alterative syntax for lists
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    53
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    54
1 :: 2 :: 3 :: Nil
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    55
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    56
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    57
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    58
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    59
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    60
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    61
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    62
val tst = "This is a " + "test\n" 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    63
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    64
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    65
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    66
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    67
println(lst.toString)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    68
println(lst.mkString("\n"))
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    69
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    70
println(lst.mkString(", "))
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    71
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    72
// some methods take more than one argument
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    73
println(lst.mkString("[", ",", "]"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    74
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    75
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    76
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    77
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    78
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    79
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    80
List(1,2,3,1).toSet
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    81
"hello".toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    82
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    83
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    84
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    85
// useful list methods
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    86
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    87
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    88
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    89
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    90
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    91
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    92
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    93
List(1,2,3,4).drop(2).sum
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    94
List(1,2,3,4,3)indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    95
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    96
"1,2,3,4,5".split(",").mkString("\n")
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    97
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    98
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
    99
// Types (slide)
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   100
//=======
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   101
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   102
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   103
 
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   104
 * some base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   105
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   106
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   107
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   108
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   109
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
   110
 * some compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   111
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   112
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   113
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   114
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   115
    List[(BigInt, String)]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   116
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   117
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   118
// Smart Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   119
//===============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   120
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   121
println(">\n\n<")
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   122
println(""">\n<""")
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   123
println("""">\n<"""")
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   124
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   125
/* in Java
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   126
val lyrics = "Sun dips down, the day has gone. \n" +
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   127
             "Witches, wolves and giants yawn. \n" +
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   128
             "Queen and dragon, troll and gnome: \n" +
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   129
             "tired buddies head for home."
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   130
*/ 
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   131
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   132
val lyrics = """Sun dips down, the day has gone.
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   133
                |Witches, wolves and giants yawn.
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   134
                |Queen and dragon, troll and gnome:
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   135
                |tired buddies head for home.""".stripMargin
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   136
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   137
println(lyrics)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   138
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   140
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   141
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   142
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   143
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   144
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   145
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   146
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   147
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   148
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   149
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   150
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   151
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   152
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   153
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   154
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   155
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   156
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   157
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   158
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   159
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   160
36
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
// The general scheme for a function: you have to give a type 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   163
// to each argument and a return type of the function
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   164
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   165
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   166
//    body 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   167
//  }
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   168
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   169
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   170
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   171
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   172
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   173
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   174
// Scala does not have a then-keyword
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   175
// both if-else branches need to be presents
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   176
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   177
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   178
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   179
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   180
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   181
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   182
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   183
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   184
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   185
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   186
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   187
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   188
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   189
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   190
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   191
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   192
def fact2(n: BigInt) : BigInt = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   194
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   195
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   196
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   197
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   198
def fib(n: Int) : Int =
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   199
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   200
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   201
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   202
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   203
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   204
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   205
def gcd(a: Int, b: Int) : Int =
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   206
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   207
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   208
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   209
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   210
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   211
def power(x: Int, n: Int) : Int =
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   212
  if (n == 0) 1  else x * power(x, n - 1) 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   213
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   214
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   215
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   216
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   217
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   218
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   219
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   220
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   221
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   222
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   223
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   224
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   225
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   226
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   227
def gcd_db(a: Int, b: Int) : Int = {
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   228
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   229
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   230
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   231
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   232
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   233
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   234
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   235
// Asserts/Testing
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   236
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   237
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   238
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   239
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   240
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   241
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   242
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   243
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   244
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   245
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   246
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   247
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   248
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   249
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   250
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   251
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   252
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   253
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   254
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   255
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   256
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   257
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   258
// the list can also be constructed in any other way
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   259
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
   260
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   261
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   262
// with if-predicates
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   263
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   264
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   265
     m <- (1 to 3).toList;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   266
     if (n + m) % 2 == 0) yield (n, m)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   267
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   268
for (n <- (1 to 3).toList; 
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   269
     m <- (1 to 3).toList;
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   270
     if ((n + m) % 2 == 0)) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   271
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   272
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   273
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   274
for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   275
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   276
for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   277
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   278
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   279
// general pattern
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   280
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   281
for (x <- ...) yield {
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   282
  // potentially complicated
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   283
  // calculation of a result
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   284
}
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   285
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   286
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   287
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   288
// with only a side-effect (no list is produced),
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   289
// has no "yield"
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 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   292
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   293
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   294
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   295
// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   296
//             (would need to have this wrapped into a function, or
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   297
//              REPL called with scala -Yrepl-class-based)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   298
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   299
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   300
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   301
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   302
// for measuring time
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   303
def time_needed[T](n: Int, code: => T) = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   304
  val start = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   305
  for (i <- (0 to n)) code
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   306
  val end = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   307
  (end - start) / 1.0e9
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   308
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   309
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   310
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   311
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   312
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   313
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   314
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   315
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   316
// mutable vs immutable factorial
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   317
def fact_mu(n: Long) : Long = {
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   318
  var result : Long = 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   319
  var i = 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   320
  while (i <= n) {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   321
    result = result * i
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   322
    i = i + 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   323
  }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   324
  result
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   325
}
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   326
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   327
def fact_im(num: Long): Long = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   328
  if (num == 1) num else
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   329
    num * fact_im(num - 1)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   330
}
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   331
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   332
def test() = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   333
  for (i <- (0 to 10).par) yield {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   334
    val l1 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_mu(n)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   335
    val l2 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_im(n) 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   336
    l1.sum - l2.sum
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   337
  }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   338
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   339
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   340
(for (i <- (1 to 10)) yield test().sum).sum
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   341
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   342
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   343
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   344
//==========
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
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   347
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   348
// obtaining a webpage
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   349
val url = """https://nms.kcl.ac.uk/christian.urban/""" 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   350
Source.fromURL(url)("ISO-8859-1").mkString
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
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   353
// another example
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   354
//val url = """http://api.postcodes.io/postcodes/CR84LQ""" 
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   355
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   356
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   357
// a function for looking up constituency data
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   358
def consty_lookup(pcode: String) : String = {
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   359
  val url = "http://api.postcodes.io/postcodes/" + pcode
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   360
  Source.fromURL(url).mkString.split(",")(16)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   361
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   362
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   363
consty_lookup("CR84LQ")
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   364
consty_lookup("WC2B4BG")
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   365
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   366
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   367
val places = 
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   368
  List("CR84LQ", "WC2B4BG", "KY169QT", "CB11LY", "CB39AX")
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   369
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   370
for (s <- places) println(consty_lookup(s))
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   371
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   372
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
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   375
// A Web Crawler 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   376
//===============
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   377
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   378
// the idea is to look for dead links using the
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   379
// regular expression "https?://[^"]*"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   380
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   381
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   382
import scala.util._
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   383
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   384
// gets the first 10K of a web-page
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   385
def get_page(url: String) : String = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   386
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   387
    getOrElse { println(s"  Problem with: $url"); ""}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   388
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   389
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   390
// regex for URLs
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   391
val http_pattern = """"https?://[^"]*"""".r
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   392
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   393
// drops the first and last character from a string
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   394
def unquote(s: String) = s.drop(1).dropRight(1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   395
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   396
def get_all_URLs(page: String): Set[String] = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   397
  http_pattern.findAllIn(page).map(unquote).toSet
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   398
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   399
// naive version of crawl - searches until a given depth,
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   400
// visits pages potentially more than once
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   401
def crawl(url: String, n: Int) : Unit = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   402
  if (n == 0) ()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   403
  else {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   404
    println(s"Visiting: $n $url")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   405
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   406
  }
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   407
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   408
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   409
// some starting URLs for the crawler
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   410
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   411
//val startURL = """https://nms.kcl.ac.uk/luc.moreau/index.html"""
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   412
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   413
crawl(startURL, 2)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   414
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   415
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   416
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   417
// Further Information
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   418
//=====================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   419
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   420
// The Scala home page and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   421
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   422
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   423
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   424
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   425
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   426
// It should be fairly easy to install the Scala binary and
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   427
// run Scala on the commandline. There are also at least 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   428
// four IDEs you can use with Scala:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   429
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   430
//  (0) Some general information about setting up IDEs
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   431
//	    with Scala support can be found at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   432
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   433
//         http://docs.scala-lang.org/getting-started.html 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   434
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   435
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   436
//  (1) Eclipse for Scala (one big bundle)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   437
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   438
//         http://scala-ide.org/download/sdk.html
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   439
//  
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   440
//  (2) IntelliJ (needs additional Plugins)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   441
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   442
//         https://www.jetbrains.com/idea/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   443
//		   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
   444
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   445
//  (3) Sublime (not free, but unlimited trial period; 
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   446
//	    needs Scala and SublimeREPL plugin)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   447
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   448
//         https://www.sublimetext.com
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   449
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   450
//  (4) Emacs (old-fashioned, but reliable)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   451
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   452
//         https://www.gnu.org/software/emacs/
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   453
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   454
//      I use the old scala-tool support for Emacs distributed at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   455
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   456
//         https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   457
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   458
//      but there is also support for the newer Ensime Scala Mode
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   459
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   460
//         http://ensime.org/editors/emacs/scala-mode/   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   461
//   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   462
// There is also Scala support in the Atom editor, but my
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   463
// experience is mixed. People also use Scala with Vim and Jedit.
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   464
// Finally there is an online editor specifically designed for 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   465
// running Scala applications (but do not blame mne if you lose all 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   466
// what you typed in):
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   467
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   468
//      https://scalafiddle.io 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   469
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   470
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   471
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   472
// 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
   473
// the very nifty feature of a Scala Worksheet -- you just save your
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   474
// file and it will be automatically evaluated and the result pasted
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   475
// into your file. However, this way of writing Scala code never worked
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   476
// for me. I just use the REPL.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   477
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   478
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   479
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   480
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   481
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   482
//  http://www.scala-lang.org/api/current/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   483
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   484
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   485
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   486
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   487
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   488
// There are also a massive number of Scala tutorials on youtube
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   489
// and there are tons of books and free material.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   490
//
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   491
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   492
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   493
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   494
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   495
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   496
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   497
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   498
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   499
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   500
// advantage of case classes
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   501
//
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   502
case class Participant(name: String, score: Int, active: Boolean)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   503
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   504
val ps = Seq(Participant("Jack", 34, true),
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   505
             Participant("Tom", 51, true),
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   506
             Participant("Bob", 90, false))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   507
             
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   508
ps.filter(_.score < 50).
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   509
   filter(_.active).
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   510
   map(_.copy(active = false))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   511
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   512
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   513
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   514
// another example why state is bad...does not work yet
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   515
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   516
// for measuring time
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   517
def time_needed[T](n: Int, code: => T) = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   518
  val start = System.nanoTime()
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   519
  for (i <- (0 to n)) code
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   520
  val end = System.nanoTime()
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   521
  (end - start) / 1.0e9
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   522
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   523
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   524
def santa_state(plan: List[Char]) : Int = {  
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   525
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   526
  var floor = 0
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   527
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   528
  for (i <- plan.par) {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   529
    if (i == '(') {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   530
      floor += 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   531
    } else {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   532
      floor -= 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   533
    }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   534
  }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   535
  
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   536
  floor
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   537
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   538
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   539
def i(c: Char) = if (c == '(') 1 else -1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   540
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   541
def santa_imutable(plan: List[Char]) : Int =
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   542
  plan.par.map(i(_)).reduce(_ + _)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   543
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   544
santa_state("(((((()))))".toList)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   545
santa_imutable("(((((()))))".toList)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   546
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   547
def randomString(length: Int) : String = 
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   548
  List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   549
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   550
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   551
santa_state(randomString(100))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   552
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   553
val large_string = randomString(3000000)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   554
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   555
time_needed(10, santa_state(large_string))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   556
time_needed(10, santa_imutable(large_string))