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