progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 11 Nov 2016 11:14:59 +0000
changeset 37 38f3868891f0
parent 36 f5ed0fef41b3
child 39 c6fe374a5fca
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
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     5
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     6
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     7
// Implicits
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     8
//===========
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     9
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    10
// for example adding your own methods to Strings:
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    11
// imagine you want to increment strings, like
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    12
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    13
//     "HAL".increment
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
// you can avoid ugly fudges, like a MyString, by
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    16
// using implicit conversions
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
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    19
implicit class MyString(s: String) {
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    20
  def increment = for (c <- s) yield (c + 1).toChar 
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    21
}
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
"HAL".increment