progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 12 Mar 2018 12:11:50 +0000
changeset 173 2de1f79dedf0
parent 170 37b1bfcdba79
child 189 ff815ca0bbcf
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
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    54
1::2::3::Nil
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" +
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
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:
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
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
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   174
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   175
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   176
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   177
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   178
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   179
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   180
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   181
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   182
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   183
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   184
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   185
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   186
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   187
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   188
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   189
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   190
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   191
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   192
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   193
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   194
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   195
def fib(n: Int): Int =
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   196
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   197
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   198
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   199
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   200
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   201
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   202
def gcd(a: Int, b: Int) : Int =
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   203
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   204
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   205
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   206
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   207
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   208
def power(x: Int, n: Int) : Int =
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   209
  if (n == 0) 1  else x * power(x, n - 1) 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   210
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   211
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   212
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   213
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   214
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   215
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   216
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   217
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   218
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   219
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   220
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   221
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
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   224
def gcd_db(a: Int, b: Int) : Int = {
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   225
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   226
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   227
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   228
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   229
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   230
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   231
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   232
// Asserts/Testing
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   233
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   234
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   235
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   236
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   237
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   238
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   239
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   240
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   241
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   242
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   243
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   244
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   245
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   246
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   247
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   248
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   249
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   250
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   251
       m <- (1 to 10).toList) yield m * n
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
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   254
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   255
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   256
// with if-predicates
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   257
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   258
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   259
     m <- (1 to 3).toList;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   260
     if (n + m) % 2 == 0) yield (n, m)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   261
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   262
for (n <- (1 to 3).toList; 
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   263
     m <- (1 to 3).toList;
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   264
     if ((n + m) % 2 == 0)) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   265
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   266
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   267
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   268
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
   269
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   270
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
   271
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   272
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   273
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   274
// with only a side-effect (no list is produced),
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   275
// has no "yield"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   276
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   277
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   278
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   279
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   280
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   281
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   282
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   283
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   284
// 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
   285
//             (would need to have this wrapped into a function, or
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   286
//              REPL called with scala -Yrepl-class-based)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   287
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   288
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   289
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   290
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   291
// for measuring time
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   292
def time_needed[T](n: Int, code: => T) = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   293
  val start = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   294
  for (i <- (0 to n)) code
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   295
  val end = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   296
  (end - start) / 1.0e9
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   297
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   298
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   299
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   300
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   301
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   302
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   303
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   304
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   305
// mutable vs immutable factorial
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   306
def fact_mu(n: Long): Long = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   307
  var result : Long = 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   308
  var i = 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   309
  while (i <= n) {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   310
    result = result * i
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   311
    i = i + 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   312
  }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   313
  result
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   314
}
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   315
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   316
def fact_im(num: Long): Long = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   317
  if (num == 1) num else
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   318
    num * fact_im(num - 1)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   319
}
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   320
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   321
def test() = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   322
  for (i <- (0 to 10).par) yield {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   323
    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
   324
    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
   325
    l1.sum - l2.sum
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   326
  }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   327
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   328
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   329
test().sum
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   330
println(l1.sum - l2.sum)
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   331
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   332
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   333
//==========
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   334
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   335
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   336
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   337
// obtaining a webpage
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   338
val url = """https://nms.kcl.ac.uk/christian.urban/""" 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   339
Source.fromURL(url)("ISO-8859-1").mkString
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   340
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   341
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   342
// another example
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   343
//val url = """http://api.postcodes.io/postcodes/CR84LQ""" 
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   344
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   345
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   346
// a function for looking up constituency data
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   347
def consty_lookup(pcode: String) : String = {
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   348
  val url = "http://api.postcodes.io/postcodes/" + pcode
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   349
  Source.fromURL(url).mkString.split(",")(16)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   350
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   351
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   352
consty_lookup("CR84LQ")
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   353
consty_lookup("WC2B4BG")
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   354
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   355
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   356
val places = 
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   357
  List("CR84LQ", "WC2B4BG", "KY169QT", "CB11LY", "CB39AX")
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   358
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   359
for (s <- places) println(consty_lookup(s))
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   360
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   361
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   362
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   363
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   364
// A Web Crawler 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   365
//===============
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   366
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   367
// the idea is to look for dead links using the
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   368
// regular expression "https?://[^"]*"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   369
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   370
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   371
import scala.util._
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   372
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   373
// gets the first 10K of a web-page
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   374
def get_page(url: String) : String = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   375
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   376
    getOrElse { println(s"  Problem with: $url"); ""}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   377
}
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
// regex for URLs
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   380
val http_pattern = """"https?://[^"]*"""".r
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   381
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   382
// drops the first and last character from a string
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   383
def unquote(s: String) = s.drop(1).dropRight(1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   384
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   385
def get_all_URLs(page: String): Set[String] = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   386
  http_pattern.findAllIn(page).map(unquote).toSet
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   387
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   388
// naive version of crawl - searches until a given depth,
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   389
// visits pages potentially more than once
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   390
def crawl(url: String, n: Int) : Unit = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   391
  if (n == 0) ()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   392
  else {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   393
    println(s"Visiting: $n $url")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   394
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 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
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   397
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   398
// some starting URLs for the crawler
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   399
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   400
//val startURL = """https://nms.kcl.ac.uk/luc.moreau/index.html"""
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   401
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   402
crawl(startURL, 2)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   403
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   404
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   405
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   406
// Further Information
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
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   409
// The Scala home page and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   410
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   411
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   412
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   413
//
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
// It should be fairly easy to install the Scala binary and
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   416
// run Scala on the commandline. There are also at least 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   417
// four IDEs you can use with Scala:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   418
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   419
//  (0) Some general information about setting up IDEs
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   420
//	    with Scala support can be found at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   421
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   422
//         http://docs.scala-lang.org/getting-started.html 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   423
//
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   424
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   425
//  (1) Eclipse for Scala (one big bundle)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   426
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   427
//         http://scala-ide.org/download/sdk.html
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
//  (2) IntelliJ (needs additional Plugins)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   430
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   431
//         https://www.jetbrains.com/idea/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   432
//		   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
   433
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   434
//  (3) Sublime (not free, but unlimited trial period; 
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   435
//	    needs Scala and SublimeREPL plugin)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   436
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   437
//         https://www.sublimetext.com
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
//  (4) Emacs (old-fashioned, but reliable)
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
//         https://www.gnu.org/software/emacs/
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   442
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   443
//      I use the old scala-tool support for Emacs distributed at
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
//         https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   446
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   447
//      but there is also support for the newer Ensime Scala Mode
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   448
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   449
//         http://ensime.org/editors/emacs/scala-mode/   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   450
//   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   451
// There is also Scala support in the Atom editor, but my
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   452
// experience is mixed. People also use Scala with Vim and Jedit.
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   453
// Finally there is an online editor specifically designed for 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   454
// running Scala applications (but do not blame mne if you lose all 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   455
// what you typed in):
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   456
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   457
//      https://scalafiddle.io 
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   458
//
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   459
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   460
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   461
// 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
   462
// the very nifty feature of a Scala Worksheet -- you just save your
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   463
// file and it will be automatically evaluated and the result pasted
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   464
// into your file. However, this way of writing Scala code never worked
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   465
// for me. I just use the REPL.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   466
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   467
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   468
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   469
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   470
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   471
//  http://www.scala-lang.org/api/current/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   472
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   473
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   474
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   475
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   476
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   477
// There are also a massive number of Scala tutorials on youtube
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   478
// and there are tons of books and free material.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   479
//
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   480
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   481
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   482
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   483
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   484
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   485
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   486
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   487
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   488
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   489
// advantage of case classes
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   490
//
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   491
case class Participant(name: String, score: Int, active: Boolean)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   492
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   493
val ps = Seq(Participant("Jack", 34, true),
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   494
             Participant("Tom", 51, true),
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   495
             Participant("Bob", 90, false))
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
ps.filter(_.score < 50).
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   498
   filter(_.active).
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   499
   map(_.copy(active = false))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   500
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
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   503
// another example why state is bad...does not work yet
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   504
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   505
// for measuring time
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   506
def time_needed[T](n: Int, code: => T) = {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   507
  val start = System.nanoTime()
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   508
  for (i <- (0 to n)) code
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   509
  val end = System.nanoTime()
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   510
  (end - start) / 1.0e9
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
def santa_state(plan: List[Char]) : Int = {  
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   514
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   515
  var floor = 0
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   516
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   517
  for (i <- plan.par) {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   518
    if (i == '(') {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   519
      floor += 1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   520
    } else {
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   521
      floor -= 1
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
  
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   525
  floor
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   526
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   527
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   528
def i(c: Char) = if (c == '(') 1 else -1
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   529
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   530
def santa_imutable(plan: List[Char]) : Int =
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   531
  plan.par.map(i(_)).reduce(_ + _)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   532
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   533
santa_state("(((((()))))".toList)
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   534
santa_imutable("(((((()))))".toList)
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
def randomString(length: Int) = 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   537
  List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
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
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   540
santa_state(randomString(100))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   541
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   542
val large_string = randomString(3000000)
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
time_needed(10, santa_state(large_string))
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   545
time_needed(10, santa_imutable(large_string))