progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 16 Nov 2016 14:37:18 +0000
changeset 50 9891c9fac37e
parent 39 c6fe374a5fca
child 51 0e60e6c24b99
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// sudoku
13
0ce25f816414 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
     2
// some none
37
38f3868891f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 36
diff changeset
     3
// pattern matching
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     4
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
     5
//type abbreviations
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
     6
type Pos = (int, Int)
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
     7
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
     8
//sorting, higher-order functions
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
     9
//lexicographic ordering
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    10
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    11
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    12
// Implicits
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    13
//===========
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    14
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    15
// for example adding your own methods to Strings:
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    16
// imagine you want to increment strings, like
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    17
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    18
//     "HAL".increment
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    19
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    20
// you can avoid ugly fudges, like a MyString, by
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    21
// using implicit conversions
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    22
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    23
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    24
implicit class MyString(s: String) {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    25
  def increment = for (c <- s) yield (c + 1).toChar 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    26
}
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    27
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    28
"HAL".increment