progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 02 Nov 2017 14:47:55 +0000
changeset 123 556cd74cbba9
parent 51 0e60e6c24b99
child 124 c45d3cd9a749
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    16
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    17
//=============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    18
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    19
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    20
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    21
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    22
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    23
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    24
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    25
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    26
// an element in a list
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    27
val lst = List(1, 2, 3, 1)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    28
lst(0)
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    29
lst(2)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    30
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    31
// some alterative syntax for lists
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    32
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    33
1::2::3::Nil
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    34
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    35
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    36
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    37
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    40
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    41
val tst = "This is a " + "test\n" 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    44
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    45
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    46
println(lst.toString)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    47
println(lst.mkString("\n"))
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    48
33
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    49
println(lst.mkString(", "))
b6cb302633eb updated
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    50
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    51
// some methods take more than one argument
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    52
println(lst.mkString("[", ",", "]"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    53
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    54
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    55
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    56
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    57
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    58
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    59
List(1,2,3,1).toSet
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    60
"hello".toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    61
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    62
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    63
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    64
// useful list methods
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    65
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    66
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    67
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    68
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    69
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    70
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    71
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    72
List(1,2,3,4).drop(2).sum
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    73
List(1,2,3,4,3)indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    74
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    75
"1,2,3,4,5".split(",").mkString("\n")
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    76
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    77
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    78
// Types
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    79
//=======
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
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    82
 
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    83
 * some base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    84
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    85
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    86
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    87
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    88
34
40424bbe109e updated
Christian Urban <urbanc@in.tum.de>
parents: 33
diff changeset
    89
 * some compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    90
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    91
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    92
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    93
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    94
    List[(BigInt, String)]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    95
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    96
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    97
// Smart Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    98
//===============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    99
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   100
println(">\n\n<")
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   101
println(""">\n<""")
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   102
println("""">\n<"""")
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   103
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   104
/* in Java
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   105
val lyrics = "Baa, Baa, Black Sheep \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   106
             "Have you any wool? \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   107
             "Yes, sir, yes sir \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   108
             "Three bags full"
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   109
*/ 
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   110
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   111
val lyrics = """Baa, Baa, Black Sheep  
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   112
                |Have you any wool?
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   113
                |Yes, sir, yes sir
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   114
                |Three bags full""".stripMargin
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   115
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   116
println(lyrics)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   117
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   118
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   119
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   120
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   121
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   122
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   124
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   125
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   126
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   127
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   128
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   129
// Hello World
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   130
//=============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   131
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   132
// an example of a stand-alone scala file;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   133
// in the coursework students must submit 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   134
// plain scala "work-sheets"
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   135
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   136
object Hello extends App { 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   137
  println("hello world")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   138
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   139
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   140
// can be called with
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   141
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   142
// $> scalac hello-world.scala
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   143
// $> scala Hello
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   144
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   145
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   146
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   147
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   148
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   149
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   150
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   154
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   155
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   156
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   157
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   158
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   159
// The general scheme for a function: you have to give a type 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   160
// to each argument and a return type of the function
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
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   163
//    body 
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
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   166
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   167
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   168
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   169
//=================
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
def fact(n: Int): Int = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   172
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   173
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   174
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   175
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   176
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   177
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   178
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   179
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   180
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   181
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   182
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   183
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   184
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   185
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   186
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   187
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   188
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   189
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   190
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   191
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   192
def fib(n: Int): Int =
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   194
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   195
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   196
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   197
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   198
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   199
def gcd(a: Int, b: Int) : Int =
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   200
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   201
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   202
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   203
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   204
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   205
def power(x: Int, n: Int) : Int =
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   206
  if (n == 0) 1  else x * power(x, n - 1) 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   207
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   208
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   209
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   210
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   211
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   212
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   213
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   214
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   215
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   216
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   217
println(s"The square of ${n} is ${square(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
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   220
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   221
def gcd_db(a: Int, b: Int) : Int = {
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   222
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   223
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   224
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   225
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   226
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   227
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   228
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   229
// Assert/Testing
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   230
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   231
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   232
assert(gcd(48, 18) == 6)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   233
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   234
assert(gcd(48, 18) == 5, "The gcd test failed")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   235
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   236
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   237
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   238
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   239
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   240
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   241
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   242
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   243
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   244
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   245
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   246
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   247
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   248
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   249
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   250
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   251
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   252
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   253
// with if-predicates
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   254
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   255
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   256
     m <- (1 to 3).toList;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   257
     if (n + m) % 2 == 0) yield (n, m)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   258
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   259
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   260
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   261
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   262
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   263
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
   264
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   265
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
   266
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   267
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   268
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   269
// with only a side-effect (no list is produced),
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   270
// has no "yield"
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
for (n <- (1 to 10)) println(n)
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
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   275
// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12.0)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   276
for (n <- (1 to 10)) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   277
for (n <- (1 to 10).par) 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
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   280
// for measuring time
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   281
def time_needed[T](i: Int, code: => T) = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   282
  val start = System.nanoTime()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   283
  for (j <- 1 to i) code
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   284
  val end = System.nanoTime()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   285
  ((end - start) / i / 1.0e9) + " secs"
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   286
}
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
val list = (1 to 1000000).toList
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   289
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   290
time_needed(10, for (n <- list.par) yield n + 42)
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   293
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   294
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   295
//==========
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   296
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   297
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   298
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   299
// obtaining a webpage
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   300
val url = """https://nms.kcl.ac.uk/christian.urban/""" 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   301
Source.fromURL(url)("ISO-8859-1").mkString
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   302
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   303
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   304
// function for looking up stockmarket data 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   305
def price_lookup(symbol: String): String = {
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   306
  val url = "https://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   307
  Source.fromURL(url).mkString.drop(1).dropRight(2)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   308
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   309
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   310
price_lookup("GOOG")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   311
price_lookup("AAPL")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   312
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   313
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   314
val companies = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   315
  List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   316
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   317
for (s <- companies) println(price_lookup(s))
32
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
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   320
// A Web Crawler 
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   321
//===============
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   322
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   323
// the idea is to look for dead links using the
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   324
// regular expression "https?://[^"]*"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   325
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   326
import io.Source
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   327
import scala.util.matching.Regex
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   328
import scala.util._
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
// gets the first 10K of a web-page
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   331
def get_page(url: String) : String = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   332
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   333
    getOrElse { println(s"  Problem with: $url"); ""}
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   336
// regex for URLs
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   337
val http_pattern = """"https?://[^"]*"""".r
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   338
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   339
// drops the first and last character from a string
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   340
def unquote(s: String) = s.drop(1).dropRight(1)
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
def get_all_URLs(page: String): Set[String] = 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   343
  http_pattern.findAllIn(page).map(unquote).toSet
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
// naive version of crawl - searches until a given depth,
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   346
// visits pages potentially more than once
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   347
def crawl(url: String, n: Int): Unit = {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   348
  if (n == 0) ()
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   349
  else {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   350
    println(s"Visiting: $n $url")
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   351
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   352
  }
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
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   355
// some starting URLs for the crawler
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   356
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   357
//val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney"""
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   358
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   359
crawl(startURL, 2)
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
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
// Further Information
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
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   366
// The Scala home page and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   367
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   368
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   369
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   370
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   371
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   372
// It should be fairly easy to install the Scala binary and
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   373
// run Scala on the commandline. There are also at least 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   374
// four IDEs you can use with Scala:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   375
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   376
//  (0) Some general information for setting up IDEs
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   377
//	    with Scala support can be found at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   378
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   379
//         http://docs.scala-lang.org/getting-started.html 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   380
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   381
//  (1) Eclipse for Scala (one big bundle)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   382
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   383
//         http://scala-ide.org/download/sdk.html
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   384
//  
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   385
//  (2) IntelliJ (needs additional Plugins)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   386
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   387
//         https://www.jetbrains.com/idea/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   388
//		   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
   389
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   390
//  (3) Sublime (not free, but unlimited trial period; 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   391
//	    needs SublimeREPL plugin)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   392
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   393
//         https://www.sublimetext.com
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   394
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   395
//  (4) Emacs (old-fashioned, but reliable)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   396
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   397
//         https://www.gnu.org/software/emacs/
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   398
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   399
//      I use the old scala-tool support for Emacs distributed at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   400
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   401
//         https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   402
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   403
//      but there is also support for the newer Ensime Scala Mode
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   404
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   405
//         http://ensime.org/editors/emacs/scala-mode/   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   406
//   
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   407
// There is also Scala support in the Atom editor, but my
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   408
// experience is mixed. People also use Scala with Vim and Jedit.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   409
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   410
// 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
   411
// the very nifty feature of a Scala Worksheet -- you just save your
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   412
// file and it will be automatically evaluated and the result pasted
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   413
// into your file. However, this way of writing Scala code never worked
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   414
// for me. I just use the REPL.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   415
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   416
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   417
// Scala Library Docs
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   418
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   419
//  http://www.scala-lang.org/api/current/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   420
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   421
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   422
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   423
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   424
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   425
// There are also a massive number of Scala tutorials on youtube
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   426
// and there are tons of books and free material.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   427
//
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   428
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   429