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