progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 16 Nov 2016 23:34:05 +0000
changeset 53 9f8751912560
parent 51 0e60e6c24b99
child 55 6610c1dfa8a9
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: 39
diff changeset
     1
// Scala Lecture 2
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     2
//=================
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     3
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     4
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     5
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     6
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     7
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     8
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     9
//in Java if something unusually happens, you return null
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    10
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    11
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    12
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    13
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    14
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    15
List(7,2,3,4,5,6).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    16
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    17
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    18
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    19
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    20
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    21
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    22
Some(1).get
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    23
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    24
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    25
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    26
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    27
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    28
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    29
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    30
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    31
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    32
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    33
// getOrElse is to set a default value
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    34
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    35
val lst = List(None, Some(1), Some(2), None, Some(3))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    36
for (x <- lst) yield x getOrElse 0
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    37
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    38
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    39
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    40
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    41
// error handling with option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    42
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    43
//  Try(something).getOrElse(what_to_do_in_an_exception)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    44
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    45
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanccc/""").mkString
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    46
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    47
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    48
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    49
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    50
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    51
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    52
Integer.parseInt("12u34")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    54
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    55
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    56
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    57
val lst = List("12345", "foo", "5432", "bar", "x21")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    58
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    59
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    60
// summing all the numbers
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    61
val sum = lst.flatMap(get_me_an_int(_)).sum
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    62
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    63
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    64
// This may not look any better than working with null in Java, but to
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    65
// see the value, you have to put yourself in the shoes of the
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    66
// consumer of the get_me_an_int function, and imagine you didn't
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    67
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    68
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    69
// In Java, if you didn't write this function, you'd have to depend on
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    70
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    71
// for the Java, you might not know that get_me_an_int could return a
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    72
// null, and your code could potentially throw a NullPointerException.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    73
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    74
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    75
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    76
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    77
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    78
// Type abbreviations
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    79
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    80
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
// some syntactic convenience
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    82
type Pos = (int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    83
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    84
type Board = List[List[Int]]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    85
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    86
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    87
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    88
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    89
// No return in Scala
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    90
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    91
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    92
//You should not use "return" in Scala:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    93
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    94
// A return expression, when evaluated, abandons the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    95
// current computation and returns to the caller of the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    96
// function in which return appears."
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    97
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    98
def sq1(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    99
def sq2(x: Int): Int = return x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   100
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   101
def sumq(ls: List[Int]): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   102
  (for (x <- ls) yield (return x * x)).sum[Int]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   103
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   104
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   105
sumq(List(1,2,3,4))
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   106
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   107
// last expression in a function is the return statement
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   108
def square(x: Int): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   109
  println(s"The argument is ${x}.")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   110
  x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   111
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   112
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   113
// Pattern Matching
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   114
//==================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   115
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   116
// A powerful tool which is supposed to come to Java in a few years
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   117
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   118
// has it for many years ;o)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   119
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   120
// The general schema:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   121
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   122
//    expression match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   123
//       case pattern1 => expression1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   124
//       case pattern2 => expression2
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   125
//       ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   126
//       case patternN => expressionN
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   127
//    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   128
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   129
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   130
// remember
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   131
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   132
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   133
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   134
def my_flatten(xs: List[Option[Int]]): List[Int] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   135
  ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   136
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   137
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   138
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   139
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   140
  case Nil => Nil
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   141
  case None::xs => my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   142
  case Some(n)::xs => n::my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   143
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   144
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   145
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   146
// another example
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   147
def get_me_a_string(n: Int): String = n match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   148
  case 0 => "zero"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   149
  case 1 => "one"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   150
  case 2 => "two"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
  case _ => "many"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   154
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   155
// Higher-Order Functions
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   156
//========================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   157
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   158
// functions can take functions as arguments
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   159
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   160
val lst = (1 to 10).toList
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   161
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   162
def even(x: Int): Boolean = x % 2 == 0
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   163
def odd(x: Int): Boolean = x % 2 == 1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   164
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   165
lst.filter(x => even(x))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   166
lst.filter(even(_))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   167
lst.filter(even)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   168
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   169
lst.find(_ > 8)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   170
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   171
def square(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   172
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   173
lst.map(square)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   174
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   175
lst.map(square).filter(_ > 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   176
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   177
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   178
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   179
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   180
// Sudoku
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   181
//========
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   182
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   183
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   184
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   185
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   186
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   187
//sorting, higher-order functions
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   188
//lexicographic ordering
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   189
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   190
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   191
// Implicits
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   192
//===========
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   194
// for example adding your own methods to Strings:
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   195
// imagine you want to increment strings, like
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   196
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   197
//     "HAL".increment
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   198
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   199
// you can avoid ugly fudges, like a MyString, by
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   200
// using implicit conversions
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   201
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   202
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   203
implicit class MyString(s: String) {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   204
  def increment = for (c <- s) yield (c + 1).toChar 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   205
}
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   206
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   207
"HAL".increment