progs/lecture4.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 30 Nov 2018 07:54:49 +0000
changeset 223 c6453f3547ec
parent 222 e52cc402caee
child 224 42d760984496
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     1
// Scala Lecture 4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     2
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     5
// Polymorphic Types
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     6
//===================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     7
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     8
// You do not want to write functions like contains, first, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     9
// length and so on for every type of lists.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    10
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    11
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    12
def length_string_list(lst: List[String]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    13
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    14
  case x::xs => 1 + length_string_list(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    15
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    16
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    17
def length_int_list(lst: List[Int]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    18
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    19
  case x::xs => 1 + length_int_list(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    20
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    21
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    22
length_string_list(List("1", "2", "3", "4"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    23
length_int_list(List(1, 2, 3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    24
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    25
//-----
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    26
def length[A](lst: List[A]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    27
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    28
  case x::xs => 1 + length(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    29
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    30
length(List("1", "2", "3", "4"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    31
length(List(1, 2, 3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    32
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    33
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    34
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    35
  case Nil => Nil
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    36
  case x::xs => f(x)::map(xs, f) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    37
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    38
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    39
map(List(1, 2, 3, 4), (x: Int) => x * x)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    40
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    41
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    42
// Remember?
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    43
def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ...
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    44
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    45
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    46
// distinct / distinctBy
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    47
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    48
val ls = List(1,2,3,3,2,4,3,2,1)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    49
ls.distinct
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    50
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    51
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    52
def distinctBy[B, C](xs: List[B], 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    53
                     f: B => C, 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    54
                     acc: List[C] = Nil): List[B] = xs match {
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  case Nil => Nil
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    56
  case x::xs => {
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
    val res = f(x)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
    if (acc.contains(res)) distinctBy(xs, f, acc)  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    else x::distinctBy(xs, f, res::acc)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  }
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
} 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    63
// distinctBy  with the identity function is 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    64
// just distinct
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    65
distinctBy(ls, (x: Int) => x)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    66
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    67
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    68
val cs = List('A', 'b', 'a', 'c', 'B', 'D', 'd')
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    69
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    70
distinctBy(cs, (c:Char) => c.toUpper)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    71
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    72
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    73
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    74
// Type inference is local in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    75
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    76
def id[T](x: T) : T = x
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    77
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    78
val x = id(322)          // Int
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    79
val y = id("hey")        // String
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    80
val z = id(Set(1,2,3,4)) // Set[Int]
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    81
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    82
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    83
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    84
// The type variable concept in Scala can get really complicated.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    85
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    86
// - variance (OO)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    87
// - bounds (subtyping)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    88
// - quantification
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    89
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    90
// Java has issues with this too: Java allows
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    91
// to write the following incorrect code, and
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    92
// only recovers by raising an exception
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    93
// at runtime.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    94
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    95
// Object[] arr = new Integer[10];
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    96
// arr[0] = "Hello World";
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    97
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    98
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    99
// Scala gives you a compile-time error
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   100
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   101
var arr = Array[Int]()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   102
arr(0) = "Hello World"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   103
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   104
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   105
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   106
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   107
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   108
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   109
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   110
// Object Oriented Programming in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
// =====================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   113
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   114
abstract class Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   115
case class Bird(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   116
case class Mammal(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   117
case class Reptile(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   118
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   119
println(Bird("Sparrow"))
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   120
println(Bird("Sparrow").toString)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   121
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   122
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   123
// you can override methods
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   124
case class Bird(name: String) extends Animal {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   125
  override def toString = name
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   126
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   127
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   128
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   129
// There is a very convenient short-hand notation
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   130
// for constructors
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   131
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   132
class Fraction(x: Int, y: Int) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   133
  def numer = x
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   134
  def denom = y
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   135
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   136
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   137
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   138
case class Fraction(numer: Int, denom: Int)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   139
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   140
val half = Fraction(1, 2)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   141
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   142
half.denom
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   143
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   144
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   145
// In mandelbrot.scala I used complex (imaginary) numbers 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   146
// and implemented the usual arithmetic operations for complex 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   147
// numbers.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   148
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   149
case class Complex(re: Double, im: Double) { 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   150
  // represents the complex number re + im * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   151
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   152
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   153
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   154
                                 this.re * that.im + that.re * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   155
  def *(that: Double) = Complex(this.re * that, this.im * that)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   156
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   157
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   158
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   159
val test = Complex(1, 2) + Complex (3, 4)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   160
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   161
// this could have equally been written as
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   162
val test = Complex(1, 2).+(Complex (3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   163
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   164
// this applies to all methods, but requires
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   165
import scala.language.postfixOps
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   166
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   167
List(5, 2, 3, 4).sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   168
List(5, 2, 3, 4) sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   169
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   170
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   171
// ...to allow the notation n + m * i
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   172
import scala.language.implicitConversions   
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   173
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   174
object i extends Complex(0, 1)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   175
implicit def double2complex(re: Double) = Complex(re, 0)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   176
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   177
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   178
val inum1 = -2.0 + -1.5 * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   179
val inum2 =  1.0 +  1.5 * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   180
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   181
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   182
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   183
// All is public by default....so no public is needed.
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   184
// You can have the usual restrictions about private 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   185
// values and methods, if you are MUTABLE !!!
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   186
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   187
case class BankAccount(init: Int) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   188
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   189
  private var balance = init
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   190
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   191
  def deposit(amount: Int): Unit = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   192
    if (amount > 0) balance = balance + amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   193
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   194
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   195
  def withdraw(amount: Int): Int =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   196
    if (0 < amount && amount <= balance) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   197
      balance = balance - amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   198
      balance
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   199
    } else throw new Error("insufficient funds")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   200
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   201
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   202
// BUT since we are completely IMMUTABLE, this is 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   203
// virtually of not concern to us.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   204
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   205
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   206
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   207
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   208
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   209
// DFAs in Scala  
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   210
import scala.util.Try
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   213
// A is the state type
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   214
// C is the input (usually characters)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   215
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   216
case class DFA[A, C](start: A,              // starting state
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   217
                     delta: (A, C) => A,    // transition function
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   218
                     fins:  A => Boolean) { // final states (Set)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   219
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   220
  def deltas(q: A, s: List[C]) : A = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   221
    case Nil => q
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   222
    case c::cs => deltas(delta(q, c), cs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   223
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   224
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   225
  def accepts(s: List[C]) : Boolean = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   226
    Try(fins(deltas(start, s))) getOrElse false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   227
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   228
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   229
// the example shown in the handout 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   230
abstract class State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   231
case object Q0 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   232
case object Q1 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   233
case object Q2 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   234
case object Q3 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   235
case object Q4 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   236
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   237
val delta : (State, Char) => State = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   238
  { case (Q0, 'a') => Q1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   239
    case (Q0, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   240
    case (Q1, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   241
    case (Q1, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   242
    case (Q2, 'a') => Q3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   243
    case (Q2, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   244
    case (Q3, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   245
    case (Q3, 'b') => Q0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   246
    case (Q4, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   247
    case (Q4, 'b') => Q4 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   248
    case _ => throw new Exception("Undefined") }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   249
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   250
val dfa = DFA(Q0, delta, Set[State](Q4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   251
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   252
dfa.accepts("abaaa".toList)     // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   253
dfa.accepts("bbabaab".toList)   // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   254
dfa.accepts("baba".toList)      // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   255
dfa.accepts("abc".toList)       // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   256
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   257
// another DFA with a Sink state
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   258
abstract class S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   259
case object S0 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   260
case object S1 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   261
case object S2 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   262
case object Sink extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   263
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   264
// transition function with a sink state
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   265
val sigma : (S, Char) => S = 
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   266
  { case (S0, 'a') => S1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   267
    case (S1, 'a') => S2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   268
    case _ => Sink
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   269
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   270
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   271
val dfa2 = DFA(S0, sigma, Set[S](S2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   272
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   273
dfa2.accepts("aa".toList)        // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   274
dfa2.accepts("".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   275
dfa2.accepts("ab".toList)        // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   276
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   277
//  we could also have a dfa for numbers
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   278
val sigmai : (S, Int) => S = 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   279
  { case (S0, 1) => S1
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   280
    case (S1, 1) => S2
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   281
    case _ => Sink
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   282
  }
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   283
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   284
val dfa3 = DFA(S0, sigmai, Set[S](S2))
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   285
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   286
dfa3.accepts(List(1, 1))        // true
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   287
dfa3.accepts(Nil)               // false
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   288
dfa3.accepts(List(1, 2))        // false
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   289
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   290
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   291
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   292
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   293
// NFAs (Nondeterministic Finite Automata)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   294
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   295
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   296
case class NFA[A, C](starts: Set[A],          // starting states
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   297
                     delta: (A, C) => Set[A], // transition function
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   298
                     fins:  A => Boolean) {   // final states 
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   299
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   300
  // given a state and a character, what is the set of 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   301
  // next states? if there is none => empty set
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   302
  def next(q: A, c: C) : Set[A] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   303
    Try(delta(q, c)) getOrElse Set[A]() 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   304
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   305
  // depth-first version of accepts
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   306
  def search(q: A, s: List[C]) : Boolean = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   307
    case Nil => fins(q)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   308
    case c::cs => next(q, c).exists(search(_, cs))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   309
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   310
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   311
  def accepts(s: List[C]) : Boolean =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   312
    starts.exists(search(_, s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   313
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   314
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   315
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   316
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   317
// NFA examples
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   318
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   319
val nfa_trans1 : (State, Char) => Set[State] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   320
  { case (Q0, 'a') => Set(Q0, Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   321
    case (Q0, 'b') => Set(Q2) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   322
    case (Q1, 'a') => Set(Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   323
    case (Q2, 'b') => Set(Q2) }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   324
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   325
val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   326
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   327
nfa.accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   328
nfa.accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   329
nfa.accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   330
nfa.accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   331
nfa.accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   332
nfa.accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   333
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   334
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   335
// Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   336
// A: Subset construction
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   337
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   338
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   339
  DFA(nfa.starts, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   340
      { case (qs, c) => nfa.nexts(qs, c) }, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   341
      _.exists(nfa.fins))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   342
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   343
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   344
subset(nfa1).accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   345
subset(nfa1).accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   346
subset(nfa1).accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   347
subset(nfa1).accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   348
subset(nfa1).accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   349
subset(nfa1).accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   350
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   351
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   352
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   353
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   354
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   355
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   356
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   357
// Cool Stuff in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   358
//=====================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   359
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   360
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   361
// Implicits or How to Pimp my Library
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   362
//=====================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   363
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   364
// For example adding your own methods to Strings:
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   365
// Imagine you want to increment strings, like
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   366
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   367
//     "HAL".increment
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   368
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   369
// you can avoid ugly fudges, like a MyString, by
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   370
// using implicit conversions.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   371
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   372
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   373
implicit class MyString(s: String) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   374
  def increment = for (c <- s) yield (c + 1).toChar 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   375
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   376
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   377
"HAL".increment
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   378
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   379
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   380
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   381
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   382
// Regular expressions - the power of DSLs in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   383
//==================================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   384
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   385
abstract class Rexp
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   386
case object ZERO extends Rexp                       // nothing
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   387
case object ONE extends Rexp                        // the empty string
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   388
case class CHAR(c: Char) extends Rexp               // a character c
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   389
case class ALT(r1: Rexp, r2: Rexp) extends Rexp     // alternative  r1 + r2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   390
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp     // sequence     r1 . r2  
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   391
case class STAR(r: Rexp) extends Rexp               // star         r*
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   392
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   393
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   394
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   395
// (ab)*
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   396
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   397
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   398
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   399
// some convenience for typing in regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   400
import scala.language.implicitConversions    
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   401
import scala.language.reflectiveCalls 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   402
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   403
def charlist2rexp(s: List[Char]): Rexp = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   404
  case Nil => ONE
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   405
  case c::Nil => CHAR(c)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   406
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   407
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   408
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   409
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   410
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   411
val r1 = STAR("ab")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   412
val r2 = STAR(ALT("ab", "baa baa black sheep"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   413
val r3 = STAR(SEQ("ab", ALT("a", "b")))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   414
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   415
implicit def RexpOps (r: Rexp) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   416
  def | (s: Rexp) = ALT(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   417
  def % = STAR(r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   418
  def ~ (s: Rexp) = SEQ(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   419
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   420
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   421
implicit def stringOps (s: String) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   422
  def | (r: Rexp) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   423
  def | (r: String) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   424
  def % = STAR(s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   425
  def ~ (r: Rexp) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   426
  def ~ (r: String) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   427
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   428
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   429
//example regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   430
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   431
val sign = "+" | "-" | ""
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   432
val number = sign ~ digit ~ digit.% 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   433
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   434
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   435
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   436
// Lazy Evaluation
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   437
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   438
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   439
// do not evaluate arguments just yet
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   440
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   441
def time_needed[T](i: Int, code: => T) = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   442
  val start = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   443
  for (j <- 1 to i) code
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   444
  val end = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   445
  (end - start)/(i * 1.0e9)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   446
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   447
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   448
// same examples using the internal regexes
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   449
val evil = "(a*)*b"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   450
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   451
("a" * 10 ++ "b").matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   452
("a" * 10).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   453
("a" * 10000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   454
("a" * 20000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   455
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   456
time_needed(2, ("a" * 10000).matches(evil))