author | Christian Urban <urbanc@in.tum.de> |
Wed, 16 Nov 2016 15:05:40 +0000 | |
changeset 51 | 0e60e6c24b99 |
parent 39 | c6fe374a5fca |
child 53 | 9f8751912560 |
permissions | -rw-r--r-- |
51 | 1 |
// Scala Lecture 2 |
2 |
//================= |
|
3 |
||
4 |
||
5 |
// Option type |
|
6 |
//============= |
|
7 |
val lst = List(None, Some(1), Some(2), None, Some(3)) |
|
8 |
||
9 |
lst.flatten |
|
10 |
Some(1).get |
|
11 |
||
12 |
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1)) |
|
13 |
||
14 |
for ((x, y) <- ps) yield { |
|
15 |
if (y == 0) None else Some(x / y) |
|
16 |
} |
|
17 |
||
8 | 18 |
// sudoku |
13
0ce25f816414
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
19 |
// some none |
37 | 20 |
// pattern matching |
36 | 21 |
|
39 | 22 |
//type abbreviations |
23 |
type Pos = (int, Int) |
|
24 |
||
25 |
//sorting, higher-order functions |
|
26 |
//lexicographic ordering |
|
36 | 27 |
|
28 |
||
29 |
// Implicits |
|
30 |
//=========== |
|
31 |
// |
|
32 |
// for example adding your own methods to Strings: |
|
33 |
// imagine you want to increment strings, like |
|
34 |
// |
|
35 |
// "HAL".increment |
|
36 |
// |
|
37 |
// you can avoid ugly fudges, like a MyString, by |
|
38 |
// using implicit conversions |
|
39 |
||
40 |
||
41 |
implicit class MyString(s: String) { |
|
42 |
def increment = for (c <- s) yield (c + 1).toChar |
|
43 |
} |
|
44 |
||
45 |
"HAL".increment |