| 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 | 
 | 
| 243 |    212 | // another example about Fractions
 | 
|  |    213 | import scala.language.implicitConversions
 | 
|  |    214 | import scala.language.reflectiveCalls
 | 
|  |    215 | 
 | 
|  |    216 | 
 | 
|  |    217 | case class Fraction(numer: Int, denom: Int) {
 | 
|  |    218 |   override def toString = numer.toString + "/" + denom.toString
 | 
|  |    219 | 
 | 
|  |    220 |   def +(other: Fraction) = Fraction(numer + other.numer, denom + other.denom)
 | 
|  |    221 |   def /(other: Fraction) = Fraction(numer * other.denom, denom * other.numer)
 | 
|  |    222 |   def /% (other: Fraction) = Fraction(numer * other.denom, denom * other.numer)
 | 
|  |    223 | 
 | 
|  |    224 | }
 | 
|  |    225 | 
 | 
|  |    226 | implicit def Int2Fraction(x: Int) = Fraction(x, 1)
 | 
|  |    227 | 
 | 
|  |    228 | 
 | 
|  |    229 | val half = Fraction(1, 2)
 | 
|  |    230 | val third = Fraction (1, 3)
 | 
|  |    231 | 
 | 
|  |    232 | half + third
 | 
|  |    233 | half / third
 | 
|  |    234 | 
 | 
|  |    235 | // not sure if one can get this to work
 | 
|  |    236 | // properly, since Scala just cannot find out
 | 
|  |    237 | // if / is for ints or for Fractions 
 | 
|  |    238 | (1 / 3) + half
 | 
|  |    239 | (1 / 2) + third
 | 
|  |    240 | 
 | 
|  |    241 | // either you have to force the Fraction-type by
 | 
|  |    242 | // using a method that is not defined for ints
 | 
|  |    243 | (1 /% 3) + half
 | 
|  |    244 | (1 /% 2) + third
 | 
|  |    245 | 
 | 
|  |    246 | 
 | 
|  |    247 | // ...or explicitly give the type in order to allow
 | 
|  |    248 | // Scala to do the conversion to Fractions 
 | 
|  |    249 | ((1:Fraction) / 3) + half
 | 
|  |    250 | (1 / (3: Fraction)) + half
 | 
|  |    251 | 
 | 
| 222 |    252 | 
 | 
|  |    253 | 
 | 
|  |    254 | // DFAs in Scala  
 | 
| 226 |    255 | //===============
 | 
| 222 |    256 | import scala.util.Try
 | 
| 218 |    257 | 
 | 
|  |    258 | 
 | 
| 222 |    259 | // A is the state type
 | 
|  |    260 | // C is the input (usually characters)
 | 
|  |    261 | 
 | 
| 223 |    262 | case class DFA[A, C](start: A,              // starting state
 | 
|  |    263 |                      delta: (A, C) => A,    // transition function
 | 
|  |    264 |                      fins:  A => Boolean) { // final states (Set)
 | 
| 222 |    265 | 
 | 
|  |    266 |   def deltas(q: A, s: List[C]) : A = s match {
 | 
|  |    267 |     case Nil => q
 | 
|  |    268 |     case c::cs => deltas(delta(q, c), cs)
 | 
|  |    269 |   }
 | 
|  |    270 | 
 | 
|  |    271 |   def accepts(s: List[C]) : Boolean = 
 | 
|  |    272 |     Try(fins(deltas(start, s))) getOrElse false
 | 
|  |    273 | }
 | 
|  |    274 | 
 | 
|  |    275 | // the example shown in the handout 
 | 
|  |    276 | abstract class State
 | 
|  |    277 | case object Q0 extends State
 | 
|  |    278 | case object Q1 extends State
 | 
|  |    279 | case object Q2 extends State
 | 
|  |    280 | case object Q3 extends State
 | 
|  |    281 | case object Q4 extends State
 | 
|  |    282 | 
 | 
|  |    283 | val delta : (State, Char) => State = 
 | 
|  |    284 |   { case (Q0, 'a') => Q1
 | 
|  |    285 |     case (Q0, 'b') => Q2
 | 
|  |    286 |     case (Q1, 'a') => Q4
 | 
|  |    287 |     case (Q1, 'b') => Q2
 | 
|  |    288 |     case (Q2, 'a') => Q3
 | 
|  |    289 |     case (Q2, 'b') => Q2
 | 
|  |    290 |     case (Q3, 'a') => Q4
 | 
|  |    291 |     case (Q3, 'b') => Q0
 | 
|  |    292 |     case (Q4, 'a') => Q4
 | 
|  |    293 |     case (Q4, 'b') => Q4 
 | 
|  |    294 |     case _ => throw new Exception("Undefined") }
 | 
|  |    295 | 
 | 
|  |    296 | val dfa = DFA(Q0, delta, Set[State](Q4))
 | 
|  |    297 | 
 | 
|  |    298 | dfa.accepts("abaaa".toList)     // true
 | 
|  |    299 | dfa.accepts("bbabaab".toList)   // true
 | 
|  |    300 | dfa.accepts("baba".toList)      // false
 | 
|  |    301 | dfa.accepts("abc".toList)       // false
 | 
|  |    302 | 
 | 
| 223 |    303 | // another DFA with a Sink state
 | 
| 222 |    304 | abstract class S
 | 
|  |    305 | case object S0 extends S
 | 
|  |    306 | case object S1 extends S
 | 
|  |    307 | case object S2 extends S
 | 
|  |    308 | case object Sink extends S
 | 
|  |    309 | 
 | 
|  |    310 | // transition function with a sink state
 | 
| 223 |    311 | val sigma : (S, Char) => S = 
 | 
| 222 |    312 |   { case (S0, 'a') => S1
 | 
|  |    313 |     case (S1, 'a') => S2
 | 
|  |    314 |     case _ => Sink
 | 
|  |    315 |   }
 | 
|  |    316 | 
 | 
|  |    317 | val dfa2 = DFA(S0, sigma, Set[S](S2))
 | 
|  |    318 | 
 | 
|  |    319 | dfa2.accepts("aa".toList)        // true
 | 
|  |    320 | dfa2.accepts("".toList)          // false
 | 
|  |    321 | dfa2.accepts("ab".toList)        // false
 | 
|  |    322 | 
 | 
| 223 |    323 | //  we could also have a dfa for numbers
 | 
|  |    324 | val sigmai : (S, Int) => S = 
 | 
|  |    325 |   { case (S0, 1) => S1
 | 
|  |    326 |     case (S1, 1) => S2
 | 
|  |    327 |     case _ => Sink
 | 
|  |    328 |   }
 | 
|  |    329 | 
 | 
|  |    330 | val dfa3 = DFA(S0, sigmai, Set[S](S2))
 | 
|  |    331 | 
 | 
|  |    332 | dfa3.accepts(List(1, 1))        // true
 | 
|  |    333 | dfa3.accepts(Nil)               // false
 | 
|  |    334 | dfa3.accepts(List(1, 2))        // false
 | 
|  |    335 | 
 | 
| 222 |    336 | 
 | 
|  |    337 | 
 | 
|  |    338 | 
 | 
|  |    339 | // NFAs (Nondeterministic Finite Automata)
 | 
|  |    340 | 
 | 
|  |    341 | 
 | 
| 223 |    342 | case class NFA[A, C](starts: Set[A],          // starting states
 | 
|  |    343 |                      delta: (A, C) => Set[A], // transition function
 | 
|  |    344 |                      fins:  A => Boolean) {   // final states 
 | 
| 222 |    345 | 
 | 
|  |    346 |   // given a state and a character, what is the set of 
 | 
|  |    347 |   // next states? if there is none => empty set
 | 
|  |    348 |   def next(q: A, c: C) : Set[A] = 
 | 
|  |    349 |     Try(delta(q, c)) getOrElse Set[A]() 
 | 
|  |    350 | 
 | 
| 242 |    351 |   def nexts(qs: Set[A], c: C) : Set[A] =
 | 
|  |    352 |     qs.flatMap(next(_, c))
 | 
|  |    353 | 
 | 
| 222 |    354 |   // depth-first version of accepts
 | 
|  |    355 |   def search(q: A, s: List[C]) : Boolean = s match {
 | 
|  |    356 |     case Nil => fins(q)
 | 
|  |    357 |     case c::cs => next(q, c).exists(search(_, cs))
 | 
|  |    358 |   }
 | 
|  |    359 | 
 | 
|  |    360 |   def accepts(s: List[C]) : Boolean =
 | 
|  |    361 |     starts.exists(search(_, s))
 | 
|  |    362 | }
 | 
|  |    363 | 
 | 
|  |    364 | 
 | 
|  |    365 | 
 | 
|  |    366 | // NFA examples
 | 
|  |    367 | 
 | 
|  |    368 | val nfa_trans1 : (State, Char) => Set[State] = 
 | 
|  |    369 |   { case (Q0, 'a') => Set(Q0, Q1) 
 | 
|  |    370 |     case (Q0, 'b') => Set(Q2) 
 | 
|  |    371 |     case (Q1, 'a') => Set(Q1) 
 | 
|  |    372 |     case (Q2, 'b') => Set(Q2) }
 | 
|  |    373 | 
 | 
|  |    374 | val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
 | 
|  |    375 | 
 | 
|  |    376 | nfa.accepts("aa".toList)             // false
 | 
|  |    377 | nfa.accepts("aaaaa".toList)          // false
 | 
|  |    378 | nfa.accepts("aaaaab".toList)         // true
 | 
|  |    379 | nfa.accepts("aaaaabbb".toList)       // true
 | 
|  |    380 | nfa.accepts("aaaaabbbaaa".toList)    // false
 | 
|  |    381 | nfa.accepts("ac".toList)             // false
 | 
|  |    382 | 
 | 
|  |    383 | 
 | 
| 223 |    384 | // Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
 | 
| 226 |    385 | // A: Subset construction. Here the state type for the DFA is
 | 
|  |    386 | //    sets of states.
 | 
| 222 |    387 | 
 | 
|  |    388 | def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
 | 
|  |    389 |   DFA(nfa.starts, 
 | 
|  |    390 |       { case (qs, c) => nfa.nexts(qs, c) }, 
 | 
|  |    391 |       _.exists(nfa.fins))
 | 
|  |    392 | }
 | 
|  |    393 | 
 | 
|  |    394 | subset(nfa1).accepts("aa".toList)             // false
 | 
|  |    395 | subset(nfa1).accepts("aaaaa".toList)          // false
 | 
|  |    396 | subset(nfa1).accepts("aaaaab".toList)         // true
 | 
|  |    397 | subset(nfa1).accepts("aaaaabbb".toList)       // true
 | 
|  |    398 | subset(nfa1).accepts("aaaaabbbaaa".toList)    // false
 | 
|  |    399 | subset(nfa1).accepts("ac".toList)             // false
 | 
|  |    400 | 
 | 
|  |    401 | 
 | 
|  |    402 | 
 | 
|  |    403 | 
 | 
|  |    404 | 
 | 
|  |    405 | 
 | 
|  |    406 | 
 | 
|  |    407 | // Cool Stuff in Scala
 | 
|  |    408 | //=====================
 | 
|  |    409 | 
 | 
|  |    410 | 
 | 
|  |    411 | // Implicits or How to Pimp my Library
 | 
|  |    412 | //=====================================
 | 
|  |    413 | //
 | 
|  |    414 | // For example adding your own methods to Strings:
 | 
|  |    415 | // Imagine you want to increment strings, like
 | 
|  |    416 | //
 | 
|  |    417 | //     "HAL".increment
 | 
|  |    418 | //
 | 
|  |    419 | // you can avoid ugly fudges, like a MyString, by
 | 
|  |    420 | // using implicit conversions.
 | 
|  |    421 | 
 | 
|  |    422 | 
 | 
|  |    423 | implicit class MyString(s: String) {
 | 
|  |    424 |   def increment = for (c <- s) yield (c + 1).toChar 
 | 
|  |    425 | }
 | 
|  |    426 | 
 | 
|  |    427 | "HAL".increment
 | 
|  |    428 | 
 | 
|  |    429 | 
 | 
|  |    430 | 
 | 
|  |    431 | // Regular expressions - the power of DSLs in Scala
 | 
|  |    432 | //==================================================
 | 
|  |    433 | 
 | 
|  |    434 | abstract class Rexp
 | 
| 226 |    435 | case object ZERO extends Rexp                     // nothing
 | 
|  |    436 | case object ONE extends Rexp                      // the empty string
 | 
|  |    437 | case class CHAR(c: Char) extends Rexp             // a character c
 | 
|  |    438 | case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
 | 
|  |    439 | case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
 | 
|  |    440 | case class STAR(r: Rexp) extends Rexp             // star         r*
 | 
| 222 |    441 | 
 | 
|  |    442 | 
 | 
|  |    443 | 
 | 
| 226 |    444 | // writing (ab)* in the format above is 
 | 
|  |    445 | // tedious
 | 
| 222 |    446 | val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
 | 
|  |    447 | 
 | 
|  |    448 | 
 | 
|  |    449 | // some convenience for typing in regular expressions
 | 
|  |    450 | import scala.language.implicitConversions    
 | 
|  |    451 | import scala.language.reflectiveCalls 
 | 
|  |    452 | 
 | 
|  |    453 | def charlist2rexp(s: List[Char]): Rexp = s match {
 | 
|  |    454 |   case Nil => ONE
 | 
|  |    455 |   case c::Nil => CHAR(c)
 | 
|  |    456 |   case c::s => SEQ(CHAR(c), charlist2rexp(s))
 | 
|  |    457 | }
 | 
| 224 |    458 | implicit def string2rexp(s: String): Rexp = 
 | 
|  |    459 |   charlist2rexp(s.toList)
 | 
| 222 |    460 | 
 | 
|  |    461 | 
 | 
|  |    462 | val r1 = STAR("ab")
 | 
|  |    463 | val r2 = STAR(ALT("ab", "baa baa black sheep"))
 | 
|  |    464 | val r3 = STAR(SEQ("ab", ALT("a", "b")))
 | 
|  |    465 | 
 | 
|  |    466 | implicit def RexpOps (r: Rexp) = new {
 | 
|  |    467 |   def | (s: Rexp) = ALT(r, s)
 | 
|  |    468 |   def % = STAR(r)
 | 
|  |    469 |   def ~ (s: Rexp) = SEQ(r, s)
 | 
|  |    470 | }
 | 
|  |    471 | 
 | 
| 226 |    472 | 
 | 
| 222 |    473 | implicit def stringOps (s: String) = new {
 | 
|  |    474 |   def | (r: Rexp) = ALT(s, r)
 | 
|  |    475 |   def | (r: String) = ALT(s, r)
 | 
|  |    476 |   def % = STAR(s)
 | 
|  |    477 |   def ~ (r: Rexp) = SEQ(s, r)
 | 
|  |    478 |   def ~ (r: String) = SEQ(s, r)
 | 
|  |    479 | }
 | 
|  |    480 | 
 | 
|  |    481 | //example regular expressions
 | 
|  |    482 | val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 | 
|  |    483 | val sign = "+" | "-" | ""
 | 
|  |    484 | val number = sign ~ digit ~ digit.% 
 | 
|  |    485 | 
 | 
|  |    486 | 
 | 
|  |    487 | 
 | 
|  |    488 | // Lazy Evaluation
 | 
|  |    489 | //=================
 | 
|  |    490 | //
 | 
| 226 |    491 | // Do not evaluate arguments just yet:
 | 
|  |    492 | // this uses the => in front of the type
 | 
|  |    493 | // of the code-argument
 | 
| 222 |    494 | 
 | 
|  |    495 | def time_needed[T](i: Int, code: => T) = {
 | 
|  |    496 |   val start = System.nanoTime()
 | 
|  |    497 |   for (j <- 1 to i) code
 | 
|  |    498 |   val end = System.nanoTime()
 | 
|  |    499 |   (end - start)/(i * 1.0e9)
 | 
|  |    500 | }
 | 
|  |    501 | 
 | 
|  |    502 | // same examples using the internal regexes
 | 
|  |    503 | val evil = "(a*)*b"
 | 
|  |    504 | 
 | 
|  |    505 | ("a" * 10 ++ "b").matches(evil)
 | 
|  |    506 | ("a" * 10).matches(evil)
 | 
|  |    507 | ("a" * 10000).matches(evil)
 | 
|  |    508 | ("a" * 20000).matches(evil)
 | 
| 226 |    509 | ("a" * 50000).matches(evil)
 | 
| 222 |    510 | 
 | 
| 226 |    511 | time_needed(1, ("a" * 50000).matches(evil))
 |