| 238 |      1 | // Scala Lecture 5
 | 
| 222 |      2 | //=================
 | 
|  |      3 | 
 | 
| 415 |      4 | // Questions at
 | 
|  |      5 | //
 | 
|  |      6 | // pollev.com/cfltutoratki576
 | 
|  |      7 | 
 | 
|  |      8 | (n, m) match {
 | 
|  |      9 |   case Pat1 =>
 | 
|  |     10 |   case _ =>
 | 
|  |     11 | }
 | 
|  |     12 | 
 | 
|  |     13 | val delta : (State, Char) => State = 
 | 
|  |     14 |   { case (Q0, 'a') => Q1
 | 
|  |     15 |     case (Q0, 'b') => Q2
 | 
|  |     16 |     case (Q1, 'a') => Q4
 | 
|  |     17 |     case (Q1, 'b') => Q2
 | 
|  |     18 |     case (Q2, 'a') => Q3
 | 
|  |     19 |     case (Q2, 'b') => Q2
 | 
|  |     20 |     case (Q3, 'a') => Q4
 | 
|  |     21 |     case (Q3, 'b') => Q0
 | 
|  |     22 |     case (Q4, 'a') => Q4
 | 
|  |     23 |     case (Q4, 'b') => Q4 
 | 
|  |     24 |     case _ => throw new Exception("Undefined") }
 | 
| 333 |     25 | 
 | 
| 222 |     26 | 
 | 
| 415 |     27 | class Foo(i: Int)
 | 
| 384 |     28 | 
 | 
| 415 |     29 | val v = new Foo(10)
 | 
|  |     30 | 
 | 
|  |     31 | case class Bar(i: Int)
 | 
|  |     32 | 
 | 
|  |     33 | val v = Bar(10)
 | 
| 384 |     34 | 
 | 
|  |     35 | 
 | 
|  |     36 | 
 | 
|  |     37 | 
 | 
|  |     38 | 
 | 
|  |     39 | 
 | 
|  |     40 | 
 | 
|  |     41 | 
 | 
|  |     42 | 
 | 
|  |     43 | 
 | 
|  |     44 | 
 | 
|  |     45 | 
 | 
| 238 |     46 | // Laziness with style
 | 
|  |     47 | //=====================
 | 
| 222 |     48 | 
 | 
| 240 |     49 | // The concept of lazy evaluation doesn’t really 
 | 
| 326 |     50 | // exist in non-functional languages. C-like languages
 | 
| 329 |     51 | // are (sort of) strict. To see the difference, consider
 | 
| 222 |     52 | 
 | 
| 238 |     53 | def square(x: Int) = x * x
 | 
| 222 |     54 | 
 | 
| 238 |     55 | square(42 + 8)
 | 
| 222 |     56 | 
 | 
| 326 |     57 | // This is called "strict evaluation".
 | 
| 222 |     58 | 
 | 
| 329 |     59 | // On the contrary, say we have a pretty expensive operation:
 | 
| 326 |     60 | 
 | 
| 238 |     61 | def peop(n: BigInt): Boolean = peop(n + 1) 
 | 
| 240 |     62 | 
 | 
| 238 |     63 | val a = "foo"
 | 
| 329 |     64 | val b = "foo"
 | 
| 222 |     65 | 
 | 
| 238 |     66 | if (a == b || peop(0)) println("true") else println("false")
 | 
| 222 |     67 | 
 | 
| 326 |     68 | // This is called "lazy evaluation":
 | 
| 238 |     69 | // you delay compuation until it is really 
 | 
| 326 |     70 | // needed. Once calculated though, the result
 | 
|  |     71 | // does not need to be re-calculated.
 | 
| 222 |     72 | 
 | 
| 326 |     73 | // A useful example is
 | 
| 328 |     74 | 
 | 
| 238 |     75 | def time_needed[T](i: Int, code: => T) = {
 | 
|  |     76 |   val start = System.nanoTime()
 | 
|  |     77 |   for (j <- 1 to i) code
 | 
|  |     78 |   val end = System.nanoTime()
 | 
|  |     79 |   f"${(end - start) / (i * 1.0e9)}%.6f secs"
 | 
| 222 |     80 | }
 | 
|  |     81 | 
 | 
| 326 |     82 | // A slightly less obvious example: Prime Numbers.
 | 
|  |     83 | // (I do not care how many) primes: 2, 3, 5, 7, 9, 11, 13 ....
 | 
| 222 |     84 | 
 | 
| 326 |     85 | def generatePrimes (s: LazyList[Int]): LazyList[Int] =
 | 
| 238 |     86 |   s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
 | 
|  |     87 | 
 | 
| 326 |     88 | val primes = generatePrimes(LazyList.from(2))
 | 
| 222 |     89 | 
 | 
| 238 |     90 | // the first 10 primes
 | 
| 329 |     91 | primes.take(100).toList
 | 
| 222 |     92 | 
 | 
| 238 |     93 | time_needed(1, primes.filter(_ > 100).take(3000).toList)
 | 
| 326 |     94 | time_needed(1, primes.filter(_ > 100).take(3000).toList)
 | 
| 222 |     95 | 
 | 
| 326 |     96 | // A Stream (LazyList) of successive numbers:
 | 
| 222 |     97 | 
 | 
| 326 |     98 | LazyList.from(2).take(10)
 | 
|  |     99 | LazyList.from(2).take(10).force
 | 
| 222 |    100 | 
 | 
| 326 |    101 | // An Iterative version of the Fibonacci numbers
 | 
|  |    102 | def fibIter(a: BigInt, b: BigInt): LazyList[BigInt] =
 | 
| 238 |    103 |   a #:: fibIter(b, a + b)
 | 
| 222 |    104 | 
 | 
|  |    105 | 
 | 
| 238 |    106 | fibIter(1, 1).take(10).force
 | 
|  |    107 | fibIter(8, 13).take(10).force
 | 
|  |    108 | 
 | 
| 326 |    109 | fibIter(1, 1).drop(10000).take(1)
 | 
|  |    110 | fibIter(1, 1).drop(10000).take(1).force
 | 
| 222 |    111 | 
 | 
|  |    112 | 
 | 
| 326 |    113 | // LazyLists are good for testing
 | 
| 222 |    114 | 
 | 
|  |    115 | 
 | 
|  |    116 | // Regular expressions - the power of DSLs in Scala
 | 
| 238 |    117 | //                                     and Laziness
 | 
| 222 |    118 | //==================================================
 | 
|  |    119 | 
 | 
|  |    120 | abstract class Rexp
 | 
| 226 |    121 | case object ZERO extends Rexp                     // nothing
 | 
|  |    122 | case object ONE extends Rexp                      // the empty string
 | 
|  |    123 | case class CHAR(c: Char) extends Rexp             // a character c
 | 
|  |    124 | case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
 | 
|  |    125 | case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
 | 
|  |    126 | case class STAR(r: Rexp) extends Rexp             // star         r*
 | 
| 222 |    127 | 
 | 
|  |    128 | 
 | 
|  |    129 | // some convenience for typing in regular expressions
 | 
|  |    130 | import scala.language.implicitConversions    
 | 
|  |    131 | import scala.language.reflectiveCalls 
 | 
|  |    132 | 
 | 
|  |    133 | def charlist2rexp(s: List[Char]): Rexp = s match {
 | 
|  |    134 |   case Nil => ONE
 | 
|  |    135 |   case c::Nil => CHAR(c)
 | 
|  |    136 |   case c::s => SEQ(CHAR(c), charlist2rexp(s))
 | 
|  |    137 | }
 | 
| 224 |    138 | implicit def string2rexp(s: String): Rexp = 
 | 
|  |    139 |   charlist2rexp(s.toList)
 | 
| 222 |    140 | 
 | 
|  |    141 | implicit def RexpOps (r: Rexp) = new {
 | 
|  |    142 |   def | (s: Rexp) = ALT(r, s)
 | 
|  |    143 |   def % = STAR(r)
 | 
|  |    144 |   def ~ (s: Rexp) = SEQ(r, s)
 | 
|  |    145 | }
 | 
|  |    146 | 
 | 
|  |    147 | implicit def stringOps (s: String) = new {
 | 
|  |    148 |   def | (r: Rexp) = ALT(s, r)
 | 
|  |    149 |   def | (r: String) = ALT(s, r)
 | 
|  |    150 |   def % = STAR(s)
 | 
|  |    151 |   def ~ (r: Rexp) = SEQ(s, r)
 | 
|  |    152 |   def ~ (r: String) = SEQ(s, r)
 | 
|  |    153 | }
 | 
|  |    154 | 
 | 
| 238 |    155 | 
 | 
| 329 |    156 | 
 | 
| 238 |    157 | 
 | 
| 222 |    158 | //example regular expressions
 | 
|  |    159 | val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 | 
|  |    160 | val sign = "+" | "-" | ""
 | 
|  |    161 | val number = sign ~ digit ~ digit.% 
 | 
|  |    162 | 
 | 
| 326 |    163 | // Task: enumerate exhaustively regular expressions
 | 
| 238 |    164 | // starting from small ones towards bigger ones.
 | 
|  |    165 | 
 | 
| 240 |    166 | // 1st idea: enumerate them all in a Set
 | 
|  |    167 | // up to a level
 | 
| 238 |    168 | 
 | 
|  |    169 | def enuml(l: Int, s: String) : Set[Rexp] = l match {
 | 
|  |    170 |   case 0 => Set(ZERO, ONE) ++ s.map(CHAR).toSet
 | 
|  |    171 |   case n =>  
 | 
|  |    172 |     val rs = enuml(n - 1, s)
 | 
|  |    173 |     rs ++
 | 
|  |    174 |     (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++
 | 
|  |    175 |     (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
 | 
|  |    176 |     (for (r1 <- rs) yield STAR(r1))
 | 
|  |    177 | }
 | 
|  |    178 | 
 | 
| 240 |    179 | enuml(1, "a")
 | 
| 238 |    180 | enuml(1, "a").size
 | 
|  |    181 | enuml(2, "a").size
 | 
| 326 |    182 | enuml(3, "a").size // out of heap space
 | 
| 238 |    183 | 
 | 
|  |    184 | 
 | 
| 326 |    185 | 
 | 
|  |    186 | def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = 
 | 
| 238 |    187 |   rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
 | 
|  |    188 |                 (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
 | 
|  |    189 |                 (for (r1 <- rs) yield STAR(r1)) )
 | 
|  |    190 | 
 | 
|  |    191 | 
 | 
| 326 |    192 | enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
 | 
| 329 |    193 | enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force
 | 
|  |    194 | 
 | 
|  |    195 | 
 | 
|  |    196 | def depth(r: Rexp) : Int = r match {
 | 
|  |    197 |   case ZERO => 0
 | 
|  |    198 |   case ONE => 0
 | 
|  |    199 |   case CHAR(_) => 0
 | 
|  |    200 |   case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
 | 
|  |    201 |   case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1 
 | 
|  |    202 |   case STAR(r1) => depth(r1) + 1
 | 
|  |    203 | }
 | 
| 238 |    204 | 
 | 
|  |    205 | 
 | 
|  |    206 | val is = 
 | 
| 326 |    207 |   (enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b')))
 | 
| 238 |    208 |     .dropWhile(depth(_) < 3)
 | 
|  |    209 |     .take(10).foreach(println))
 | 
|  |    210 | 
 | 
|  |    211 | 
 | 
| 328 |    212 | 
 | 
| 326 |    213 | // (Immutable)
 | 
|  |    214 | // Object Oriented Programming in Scala
 | 
|  |    215 | // =====================================
 | 
| 238 |    216 | 
 | 
| 329 |    217 | 
 | 
|  |    218 | abstract class Animal 
 | 
| 326 |    219 | case class Bird(name: String) extends Animal {
 | 
|  |    220 |    override def toString = name
 | 
|  |    221 | }
 | 
|  |    222 | case class Mammal(name: String) extends Animal
 | 
|  |    223 | case class Reptile(name: String) extends Animal
 | 
|  |    224 | 
 | 
|  |    225 | Mammal("Zebra")
 | 
|  |    226 | println(Mammal("Zebra"))
 | 
|  |    227 | println(Mammal("Zebra").toString)
 | 
|  |    228 | 
 | 
| 238 |    229 | 
 | 
| 326 |    230 | Bird("Sparrow")
 | 
|  |    231 | println(Bird("Sparrow"))
 | 
|  |    232 | println(Bird("Sparrow").toString)
 | 
|  |    233 | 
 | 
| 383 |    234 | Bird("Sparrow").copy(name = "House Sparrow")
 | 
|  |    235 | 
 | 
|  |    236 | def group(a : Animal) = a match {
 | 
|  |    237 |   case Bird(_) => "It's a bird"
 | 
|  |    238 |   case Mammal(_) => "It's a mammal"
 | 
|  |    239 | }
 | 
|  |    240 | 
 | 
| 326 |    241 | 
 | 
|  |    242 | // There is a very convenient short-hand notation
 | 
|  |    243 | // for constructors:
 | 
|  |    244 | 
 | 
|  |    245 | class Fraction(x: Int, y: Int) {
 | 
|  |    246 |   def numer = x
 | 
|  |    247 |   def denom = y
 | 
| 238 |    248 | }
 | 
|  |    249 | 
 | 
| 326 |    250 | val half = new Fraction(1, 2)
 | 
| 383 |    251 | half.numer
 | 
| 326 |    252 | 
 | 
|  |    253 | case class Fraction(numer: Int, denom: Int)
 | 
|  |    254 | 
 | 
|  |    255 | val half = Fraction(1, 2)
 | 
|  |    256 | 
 | 
| 383 |    257 | half.numer
 | 
| 326 |    258 | half.denom
 | 
|  |    259 | 
 | 
|  |    260 | 
 | 
|  |    261 | // In mandelbrot.scala I used complex (imaginary) numbers 
 | 
|  |    262 | // and implemented the usual arithmetic operations for complex 
 | 
|  |    263 | // numbers.
 | 
|  |    264 | 
 | 
|  |    265 | case class Complex(re: Double, im: Double) { 
 | 
|  |    266 |   // represents the complex number re + im * i
 | 
|  |    267 |   def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
 | 
|  |    268 |   def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
 | 
|  |    269 |   def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
 | 
|  |    270 |                                  this.re * that.im + that.re * this.im)
 | 
|  |    271 |   def *(that: Double) = Complex(this.re * that, this.im * that)
 | 
|  |    272 |   def abs = Math.sqrt(this.re * this.re + this.im * this.im)
 | 
|  |    273 | }
 | 
|  |    274 | 
 | 
|  |    275 | val test = Complex(1, 2) + Complex (3, 4)
 | 
|  |    276 | 
 | 
| 383 |    277 | import scala.language.postfixOps
 | 
|  |    278 | List(5,4,3,2,1).sorted.reverse
 | 
|  |    279 | 
 | 
| 326 |    280 | // this could have equally been written as
 | 
|  |    281 | val test = Complex(1, 2).+(Complex (3, 4))
 | 
|  |    282 | 
 | 
|  |    283 | // this applies to all methods, but requires
 | 
|  |    284 | import scala.language.postfixOps
 | 
|  |    285 | 
 | 
|  |    286 | List(5, 2, 3, 4).sorted
 | 
|  |    287 | List(5, 2, 3, 4) sorted
 | 
|  |    288 | 
 | 
|  |    289 | 
 | 
|  |    290 | // ...to allow the notation n + m * i
 | 
|  |    291 | import scala.language.implicitConversions   
 | 
|  |    292 | 
 | 
|  |    293 | val i = Complex(0, 1)
 | 
|  |    294 | implicit def double2complex(re: Double) = Complex(re, 0)
 | 
|  |    295 | 
 | 
| 238 |    296 | 
 | 
| 326 |    297 | val inum1 = -2.0 + -1.5 * i
 | 
|  |    298 | val inum2 =  1.0 +  1.5 * i
 | 
|  |    299 | 
 | 
|  |    300 | 
 | 
|  |    301 | 
 | 
|  |    302 | // All is public by default....so no public is needed.
 | 
|  |    303 | // You can have the usual restrictions about private 
 | 
|  |    304 | // values and methods, if you are MUTABLE !!!
 | 
|  |    305 | 
 | 
|  |    306 | case class BankAccount(init: Int) {
 | 
|  |    307 | 
 | 
|  |    308 |   private var balance = init
 | 
|  |    309 | 
 | 
|  |    310 |   def deposit(amount: Int): Unit = {
 | 
|  |    311 |     if (amount > 0) balance = balance + amount
 | 
|  |    312 |   }
 | 
| 238 |    313 | 
 | 
| 326 |    314 |   def withdraw(amount: Int): Int =
 | 
|  |    315 |     if (0 < amount && amount <= balance) {
 | 
|  |    316 |       balance = balance - amount
 | 
|  |    317 |       balance
 | 
|  |    318 |     } else throw new Error("insufficient funds")
 | 
| 238 |    319 | }
 | 
|  |    320 | 
 | 
| 326 |    321 | // BUT since we are completely IMMUTABLE, this is 
 | 
| 383 |    322 | // virtually of no concern to us.
 | 
| 326 |    323 | 
 | 
|  |    324 | 
 | 
|  |    325 | 
 | 
|  |    326 | // another example about Fractions
 | 
|  |    327 | import scala.language.implicitConversions
 | 
|  |    328 | import scala.language.reflectiveCalls
 | 
|  |    329 | 
 | 
|  |    330 | case class Fraction(numer: Int, denom: Int) {
 | 
|  |    331 |   override def toString = numer.toString + "/" + denom.toString
 | 
|  |    332 | 
 | 
| 383 |    333 |   def +(other: Fraction) = 
 | 
|  |    334 |     Fraction(numer * other.denom + other.numer * denom, 
 | 
|  |    335 |              denom * other.denom)
 | 
|  |    336 |   def *(other: Fraction) = Fraction(numer * other.numer, denom * other.denom)
 | 
| 326 |    337 |  }
 | 
|  |    338 | 
 | 
|  |    339 | implicit def Int2Fraction(x: Int) = Fraction(x, 1)
 | 
|  |    340 | 
 | 
|  |    341 | val half = Fraction(1, 2)
 | 
|  |    342 | val third = Fraction (1, 3)
 | 
|  |    343 | 
 | 
|  |    344 | half + third
 | 
| 383 |    345 | half * third
 | 
| 326 |    346 | 
 | 
| 383 |    347 | 1 + half
 | 
|  |    348 | 
 | 
|  |    349 | 
 | 
| 326 |    350 | 
 | 
|  |    351 | 
 | 
|  |    352 | // DFAs in Scala  
 | 
|  |    353 | //===============
 | 
|  |    354 | import scala.util.Try
 | 
| 238 |    355 | 
 | 
| 326 |    356 | 
 | 
|  |    357 | // A is the state type
 | 
|  |    358 | // C is the input (usually characters)
 | 
|  |    359 | 
 | 
|  |    360 | case class DFA[A, C](start: A,              // starting state
 | 
|  |    361 |                      delta: (A, C) => A,    // transition function
 | 
|  |    362 |                      fins:  A => Boolean) { // final states (Set)
 | 
|  |    363 | 
 | 
|  |    364 |   def deltas(q: A, s: List[C]) : A = s match {
 | 
|  |    365 |     case Nil => q
 | 
|  |    366 |     case c::cs => deltas(delta(q, c), cs)
 | 
|  |    367 |   }
 | 
|  |    368 | 
 | 
|  |    369 |   def accepts(s: List[C]) : Boolean = 
 | 
| 383 |    370 |     Try(fins(deltas(start, s))).getOrElse(false)
 | 
| 238 |    371 | }
 | 
|  |    372 | 
 | 
| 326 |    373 | // the example shown in the handout 
 | 
|  |    374 | abstract class State
 | 
|  |    375 | case object Q0 extends State
 | 
|  |    376 | case object Q1 extends State
 | 
|  |    377 | case object Q2 extends State
 | 
|  |    378 | case object Q3 extends State
 | 
|  |    379 | case object Q4 extends State
 | 
| 238 |    380 | 
 | 
| 326 |    381 | val delta : (State, Char) => State = 
 | 
|  |    382 |   { case (Q0, 'a') => Q1
 | 
|  |    383 |     case (Q0, 'b') => Q2
 | 
|  |    384 |     case (Q1, 'a') => Q4
 | 
|  |    385 |     case (Q1, 'b') => Q2
 | 
|  |    386 |     case (Q2, 'a') => Q3
 | 
|  |    387 |     case (Q2, 'b') => Q2
 | 
|  |    388 |     case (Q3, 'a') => Q4
 | 
|  |    389 |     case (Q3, 'b') => Q0
 | 
|  |    390 |     case (Q4, 'a') => Q4
 | 
|  |    391 |     case (Q4, 'b') => Q4 
 | 
|  |    392 |     case _ => throw new Exception("Undefined") }
 | 
|  |    393 | 
 | 
|  |    394 | val dfa = DFA(Q0, delta, Set[State](Q4))
 | 
|  |    395 | 
 | 
|  |    396 | dfa.accepts("abaaa".toList)     // true
 | 
|  |    397 | dfa.accepts("bbabaab".toList)   // true
 | 
|  |    398 | dfa.accepts("baba".toList)      // false
 | 
|  |    399 | dfa.accepts("abc".toList)       // false
 | 
|  |    400 | 
 | 
| 238 |    401 | 
 | 
| 326 |    402 | // NFAs (Nondeterministic Finite Automata)
 | 
|  |    403 | 
 | 
|  |    404 | 
 | 
|  |    405 | case class NFA[A, C](starts: Set[A],          // starting states
 | 
|  |    406 |                      delta: (A, C) => Set[A], // transition function
 | 
|  |    407 |                      fins:  A => Boolean) {   // final states 
 | 
|  |    408 | 
 | 
|  |    409 |   // given a state and a character, what is the set of 
 | 
|  |    410 |   // next states? if there is none => empty set
 | 
|  |    411 |   def next(q: A, c: C) : Set[A] = 
 | 
| 383 |    412 |     Try(delta(q, c)).getOrElse(Set[A]()) 
 | 
| 326 |    413 | 
 | 
|  |    414 |   def nexts(qs: Set[A], c: C) : Set[A] =
 | 
|  |    415 |     qs.flatMap(next(_, c))
 | 
|  |    416 | 
 | 
|  |    417 |   // depth-first version of accepts
 | 
|  |    418 |   def search(q: A, s: List[C]) : Boolean = s match {
 | 
|  |    419 |     case Nil => fins(q)
 | 
|  |    420 |     case c::cs => next(q, c).exists(search(_, cs))
 | 
|  |    421 |   }
 | 
|  |    422 | 
 | 
|  |    423 |   def accepts(s: List[C]) : Boolean =
 | 
|  |    424 |     starts.exists(search(_, s))
 | 
| 238 |    425 | }
 | 
|  |    426 | 
 | 
|  |    427 | 
 | 
| 326 |    428 | 
 | 
|  |    429 | // NFA examples
 | 
|  |    430 | 
 | 
|  |    431 | val nfa_trans1 : (State, Char) => Set[State] = 
 | 
|  |    432 |   { case (Q0, 'a') => Set(Q0, Q1) 
 | 
|  |    433 |     case (Q0, 'b') => Set(Q2) 
 | 
|  |    434 |     case (Q1, 'a') => Set(Q1) 
 | 
|  |    435 |     case (Q2, 'b') => Set(Q2) }
 | 
| 238 |    436 | 
 | 
| 326 |    437 | val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
 | 
| 238 |    438 | 
 | 
| 326 |    439 | nfa.accepts("aa".toList)             // false
 | 
|  |    440 | nfa.accepts("aaaaa".toList)          // false
 | 
|  |    441 | nfa.accepts("aaaaab".toList)         // true
 | 
|  |    442 | nfa.accepts("aaaaabbb".toList)       // true
 | 
|  |    443 | nfa.accepts("aaaaabbbaaa".toList)    // false
 | 
|  |    444 | nfa.accepts("ac".toList)             // false
 | 
| 222 |    445 | 
 | 
| 238 |    446 | 
 | 
| 326 |    447 | // Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
 | 
|  |    448 | // A: Subset construction. Here the state type for the DFA is
 | 
|  |    449 | //    sets of states.
 | 
| 238 |    450 | 
 | 
| 383 |    451 | 
 | 
| 326 |    452 | def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
 | 
|  |    453 |   DFA(nfa.starts, 
 | 
|  |    454 |       { case (qs, c) => nfa.nexts(qs, c) }, 
 | 
|  |    455 |       _.exists(nfa.fins))
 | 
| 238 |    456 | }
 | 
|  |    457 | 
 | 
| 326 |    458 | subset(nfa).accepts("aa".toList)             // false
 | 
|  |    459 | subset(nfa).accepts("aaaaa".toList)          // false
 | 
|  |    460 | subset(nfa).accepts("aaaaab".toList)         // true
 | 
|  |    461 | subset(nfa).accepts("aaaaabbb".toList)       // true
 | 
|  |    462 | subset(nfa).accepts("aaaaabbbaaa".toList)    // false
 | 
|  |    463 | subset(nfa).accepts("ac".toList)             // false
 | 
| 238 |    464 | 
 | 
| 384 |    465 | import scala.math.pow
 | 
|  |    466 | 
 | 
|  |    467 | 
 | 
|  |    468 | 
 | 
|  |    469 | 
 | 
|  |    470 | 
 | 
|  |    471 | 
 | 
|  |    472 | 
 | 
|  |    473 | 
 | 
|  |    474 | 
 | 
|  |    475 | 
 | 
|  |    476 | 
 | 
| 238 |    477 | 
 | 
| 222 |    478 | 
 | 
| 240 |    479 | // The End ... Almost Christmas
 | 
| 238 |    480 | //===============================
 | 
|  |    481 | 
 | 
|  |    482 | // I hope you had fun!
 | 
|  |    483 | 
 | 
|  |    484 | // A function should do one thing, and only one thing.
 | 
|  |    485 | 
 | 
|  |    486 | // Make your variables immutable, unless there's a good 
 | 
| 326 |    487 | // reason not to. Usually there is not.
 | 
| 238 |    488 | 
 | 
| 326 |    489 | // I did it once, but this is actually not a good reason:
 | 
| 240 |    490 | // generating new labels:
 | 
|  |    491 | 
 | 
| 238 |    492 | var counter = -1
 | 
| 222 |    493 | 
 | 
| 238 |    494 | def Fresh(x: String) = {
 | 
|  |    495 |   counter += 1
 | 
|  |    496 |   x ++ "_" ++ counter.toString()
 | 
|  |    497 | }
 | 
|  |    498 | 
 | 
|  |    499 | Fresh("x")
 | 
|  |    500 | Fresh("x")
 | 
|  |    501 | 
 | 
|  |    502 | 
 | 
|  |    503 | 
 | 
| 326 |    504 | // I think you can be productive on Day 1, but the 
 | 
|  |    505 | // language is deep.
 | 
| 238 |    506 | //
 | 
|  |    507 | // http://scalapuzzlers.com
 | 
|  |    508 | //
 | 
|  |    509 | // http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/
 | 
|  |    510 | 
 | 
| 328 |    511 | val two   = 0.2
 | 
|  |    512 | val one   = 0.1
 | 
|  |    513 | val eight = 0.8
 | 
|  |    514 | val six   = 0.6
 | 
|  |    515 | 
 | 
|  |    516 | two - one == one
 | 
|  |    517 | eight - six == two
 | 
| 329 |    518 | eight - six
 | 
| 328 |    519 | 
 | 
|  |    520 | 
 | 
| 329 |    521 | // problems about equality and type-errors
 | 
| 328 |    522 | 
 | 
| 329 |    523 | List(1, 2, 3).contains("your cup")   // should not compile, but retruns false
 | 
|  |    524 | 
 | 
|  |    525 | List(1, 2, 3) == Vector(1, 2, 3)     // again should not compile, but returns true
 | 
| 326 |    526 | 
 | 
| 238 |    527 | 
 | 
|  |    528 | // I like best about Scala that it lets me often write
 | 
|  |    529 | // concise, readable code. And it hooks up with the 
 | 
| 326 |    530 | // Isabelle theorem prover. 
 | 
|  |    531 | 
 | 
|  |    532 | 
 | 
|  |    533 | // Puzzlers
 | 
|  |    534 | 
 | 
| 329 |    535 | val month = 12
 | 
|  |    536 | val day = 24
 | 
|  |    537 | val (hour, min, sec) = (12, 0, 0)
 | 
| 326 |    538 | 
 | 
|  |    539 | // use lowercase names for variable 
 | 
|  |    540 | 
 | 
|  |    541 | 
 | 
|  |    542 | //==================
 | 
|  |    543 | val oneTwo = Seq(1, 2, 3).permutations
 | 
|  |    544 | 
 | 
|  |    545 | if (oneTwo.length > 0) {
 | 
| 329 |    546 |   println("Permutations of 1,2 and 3:")
 | 
| 326 |    547 |   oneTwo.foreach(println)
 | 
|  |    548 | }
 | 
|  |    549 | 
 | 
|  |    550 | val threeFour = Seq(3, 4, 5).permutations
 | 
|  |    551 | 
 | 
|  |    552 | if (!threeFour.isEmpty) {
 | 
| 329 |    553 |   println("Permutations of 3, 4 and 5:")
 | 
| 326 |    554 |   threeFour.foreach(println)
 | 
|  |    555 | }
 | 
| 238 |    556 | 
 | 
| 326 |    557 | //==================
 | 
|  |    558 | val (a, b, c) =
 | 
|  |    559 |     if (4 < 5) {
 | 
|  |    560 |         "bar"
 | 
|  |    561 |     } else { 
 | 
|  |    562 |         Some(10)
 | 
|  |    563 |     }
 | 
|  |    564 | 
 | 
|  |    565 | //Because when an expression has multiple return branches, Scala tries to
 | 
|  |    566 | //be helpful, by picking the first common ancestor type of all the
 | 
|  |    567 | //branches as the type of the whole expression.
 | 
|  |    568 | //
 | 
|  |    569 | //In this case, one branch has type String and the other has type
 | 
|  |    570 | //Option[Int], so the compiler decides that what the developer really
 | 
|  |    571 | //wants is for the whole if/else expression to have type Serializable,
 | 
|  |    572 | //since that’s the most specific type to claim both String and Option as
 | 
|  |    573 | //descendants.
 | 
|  |    574 | //
 | 
|  |    575 | //And guess what, Tuple3[A, B, C] is also Serializable, so as far as the
 | 
|  |    576 | //compiler is concerned, the assignment of the whole mess to (a, b, c)
 | 
|  |    577 | //can’t be proven invalid. So it gets through with a warning,
 | 
|  |    578 | //destined to fail at runtime.
 | 
|  |    579 | 
 | 
|  |    580 | 
 | 
|  |    581 | //================
 | 
|  |    582 | // does not work anymore in 2.13.0
 | 
| 329 |    583 | val numbers = List("1", "2").toSet + "3"
 |