progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 10 Nov 2016 04:02:45 +0000
changeset 32 45557ad18ea6
parent 26 a7afc2540a88
child 33 b6cb302633eb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
     1
// Lecture 1
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
     5
// (variable names should be lower case)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
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
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    11
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    12
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    13
//=============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    14
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    15
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    16
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    17
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    18
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    19
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    20
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    21
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    22
// an element in a list
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    23
List(1, 2, 3, 1)(0)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    24
List(1, 2, 3, 1)(3)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    25
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    26
1::2::3::Nil
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    27
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    28
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    29
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    30
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    31
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    32
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    33
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    34
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
    35
val tst = "This is a " ++ "test\n" 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    36
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    37
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    40
println(lst.toString)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
println(lst.mkString("\n"))
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
// some methods take more than one argument
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    44
println(lst.mkString("[", ",", "]"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    45
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    46
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    47
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    48
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    49
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    50
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    51
List(1,2,3,1).toSet
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    52
"hello".toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    53
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    54
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    55
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    56
// useful list methods
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    57
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    58
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    59
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    60
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    61
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    62
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    63
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    64
List(1,2,3,4).drop(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    65
List(1,2,3,4,3).indexOf(3)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    66
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    67
"1,2,3,4,5".split(",").toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    68
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    69
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    70
// Types
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    71
//=======
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    72
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    73
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    74
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    75
 * Base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    76
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    77
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    78
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    79
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    80
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    81
 * Compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    82
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    83
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    84
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    85
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    86
    List[(BigInt, String)]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    87
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    88
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    89
// Smart Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    90
//===============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    91
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    92
println(">\n<")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    93
println(""">\n<""")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    94
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    95
/* in Java
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    96
val lyrics = "Baa, Baa, Black Sheep \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    97
             "Have you any wool? \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    98
             "Yes, sir, yes sir \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    99
             "Three bags full"
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   100
*/ 
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   101
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   102
val lyrics = """Baa, Baa, Black Sheep  
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   103
                |Have you any wool?
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   104
                |Yes, sir, yes sir
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   105
                |Three bags full""".stripMargin
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   106
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   107
println(lyrics)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   108
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   109
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   110
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   111
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   112
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   113
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   114
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   115
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   116
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   117
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   118
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   119
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   120
// Hello World
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   121
//=============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   122
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   123
// an example of a stand-alone scala file;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   124
// in the coursework students must submit 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   125
// plain scala "work-sheets"
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   126
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   127
object Hello extends App { 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   128
  println("hello world")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   129
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   130
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   131
// can be called with
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   132
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   133
// $> scalac hello-world.scala
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   134
// $> scala Hello
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   135
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   136
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   137
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   138
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   139
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   140
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   141
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   142
def square(x: Int): Int = x * x
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   143
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   144
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   145
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   146
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   147
// If control structure
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   148
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   149
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   150
def fact(n: Int): Int = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   151
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   152
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   153
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   154
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   155
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   156
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   157
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   158
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   159
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   160
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   161
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   162
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   163
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   164
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   165
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   166
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   167
def fib(n: Int): Int =
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   168
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   169
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   170
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   171
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   172
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   173
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   174
def gcd(a: Int, b: Int): Int =
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   175
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   176
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   177
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   178
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   179
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   180
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   181
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   182
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   183
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   184
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   185
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   186
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   187
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   188
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   189
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   190
def gcd_db(a: Int, b: Int): Int = {
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   191
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   192
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   193
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   194
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   195
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   196
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   197
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   198
// Assert/Testing
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   199
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   200
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   201
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   202
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   203
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   204
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   205
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   206
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   207
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   208
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   209
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   210
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   211
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   212
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   213
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   214
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   215
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   216
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   217
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   218
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   219
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   220
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   221
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   222
// with if-predicates
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   223
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   224
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   225
     m <- (1 to 3).toList;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   226
     if (n + m) % 2 == 0) yield (n, m)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   227
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   228
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   229
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   230
// with patterns
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
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
   233
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   234
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
   235
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   236
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   237
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   238
// with only a side-effect (no list is produced)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   239
// has no "yield"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   240
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   241
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   242
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   243
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   244
// concurrency (ONLY WORKS IN 2.11.8)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   245
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   246
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   247
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   248
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   249
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   250
// for testing time
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   251
def time_needed[T](i: Int, code: => T) = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   252
  val start = System.nanoTime()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   253
  for (j <- 1 to i) code
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   254
  val end = System.nanoTime()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   255
  ((end - start) / i / 1.0e9) + " secs"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   256
}
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
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   259
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   260
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   261
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   262
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   263
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   264
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   265
//==========
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   266
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   267
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   268
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   269
val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   270
Source.fromURL(url)("ISO-8859-1").mkString
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   271
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   272
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   273
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   274
def price_lookup(symbol: String): String = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   275
  val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   276
  Source.fromURL(url).mkString.drop(1).dropRight(2)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   277
}
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
price_lookup("GOOG")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   280
price_lookup("AAPL")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   281
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   282
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   283
val companies = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   284
  List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   285
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   286
for (s <- companies.par) println(price_lookup(s))
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   287
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   288
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   289
// A Web Crawler
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   292
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   293
import scala.util.matching.Regex
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   294
import scala.util._
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   295
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   296
// gets the first 10K of a web-page
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   297
def get_page(url: String) : String = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   298
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   299
    getOrElse { println(s"  Problem with: $url"); ""}
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   302
// regex for URLs
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   303
val http_pattern = """"https?://[^"]*"""".r
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   304
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   305
// drops the first and last character from a string
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   306
def unquote(s: String) = s.drop(1).dropRight(1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   307
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   308
def get_all_URLs(page: String): Set[String] = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   309
  http_pattern.findAllIn(page).map(unquote).toSet
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   310
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   311
// naive version of crawl - searches until a given depth,
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   312
// visits pages potentially more than once
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   313
def crawl(url: String, n: Int): Unit = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   314
  if (n == 0) ()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   315
  else {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   316
    println(s"Visiting: $n $url")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   317
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   318
  }
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   319
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   320
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   321
// some starting URLs for the crawler
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   322
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc"""
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   323
//val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney"""
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   324
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   325
crawl(startURL, 2)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   326
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   327
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   328
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   329
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   330
// Adding your own methods to Strings
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   331
//====================================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   332
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   333
// imagine you want to implement an additional
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   334
// method to strings, like
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   335
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   336
//     "HAL".increment
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   337
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   338
// you can avoid ugly fudges, like a MyString
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   339
// class by using implicit conversions
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   342
implicit class MyString(s: String) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   343
  def increment = for (c <- s) yield (c + 1).toChar 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   344
}
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
"HAL".increment
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   347
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   348
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   349
// Further Information
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   352
// Scala download
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   353
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   354
//  http://www.scala-lang.org
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   355
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   356
// Eclipse for Scala
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   357
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   358
//  http://scala-ide.org/download/sdk.html
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   359
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   360
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   361
// library docs
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
//  http://www.scala-lang.org/api/current/
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   364
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   365
// tutorials
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   366
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   367
//  http://docs.scala-lang.org/tutorials/