equal
  deleted
  inserted
  replaced
  
    
    
     1 // Scala Lecture 4  | 
     1 // Scala Lecture 4  | 
     2 //=================  | 
     2 //=================  | 
         | 
     3   | 
         | 
     4 // tail-recursion  | 
         | 
     5 // polymorphic types  | 
         | 
     6 // implicits  | 
         | 
     7   | 
         | 
     8 import scala.annotation.tailrec  | 
         | 
     9   | 
         | 
    10 @tailrec   | 
         | 
    11 def fact(n: BigInt): BigInt =   | 
         | 
    12   if (n == 0) 1 else n * fact(n - 1)  | 
         | 
    13         | 
         | 
    14 @tailrec        | 
         | 
    15 def factT(n: BigInt, acc: BigInt): BigInt =  | 
         | 
    16   if (n == 0) acc else factT(n - 1, n * acc)  | 
         | 
    17   | 
         | 
    18   | 
         | 
    19 println(factT(1000000), 1))   | 
         | 
    20   | 
         | 
    21   | 
         | 
    22 def foo[A](args: List[A]) = ???  | 
         | 
    23   | 
         | 
    24 foo(List("1","2","3","4")) | 
         | 
    25 import scala.annotation.tailrec  | 
         | 
    26   | 
         | 
    27   | 
         | 
    28 // from knight1.scala  | 
         | 
    29 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???  | 
         | 
    30   | 
         | 
    31 // should be  | 
         | 
    32 def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ???  | 
     3   | 
    33   | 
     4   | 
    34   | 
     5 // expressions (essentially trees)  | 
    35 // expressions (essentially trees)  | 
     6   | 
    36   | 
     7 abstract class Exp  | 
    37 abstract class Exp  | 
   250   | 
   280   | 
   251   | 
   281   | 
   252 // Tail recursion  | 
   282 // Tail recursion  | 
   253 //================  | 
   283 //================  | 
   254   | 
   284   | 
   255 @tailrec  | 
         | 
   256 def fact(n: BigInt): BigInt =   | 
   285 def fact(n: BigInt): BigInt =   | 
   257   if (n == 0) 1 else n * fact(n - 1)  | 
   286   if (n == 0) 1 else n * fact(n - 1)  | 
   258   | 
   287   | 
   259   | 
   288   | 
   260 fact(10)            | 
   289 fact(10)            | 
   517   | 
   546   | 
   518 implicit class MyString(s: String) { | 
   547 implicit class MyString(s: String) { | 
   519   def increment = s.map(c => (c + 1).toChar)   | 
   548   def increment = s.map(c => (c + 1).toChar)   | 
   520 }  | 
   549 }  | 
   521   | 
   550   | 
   522 "HAL".increment  | 
   551 "HAL".increment.map(_.toInt)  | 
   523   | 
   552   | 
   524   | 
   553   | 
   525 // Abstract idea:  | 
   554 // Abstract idea:  | 
   526 // In that version implicit conversions were used to solve the   | 
   555 // In that version implicit conversions were used to solve the   | 
   527 // late extension problem; namely, given a class C and a class T,   | 
   556 // late extension problem; namely, given a class C and a class T,   | 
   578 }  | 
   607 }  | 
   579   | 
   608   | 
   580 implicit def string2rexp(s: String): Rexp =   | 
   609 implicit def string2rexp(s: String): Rexp =   | 
   581   charlist2rexp(s.toList)  | 
   610   charlist2rexp(s.toList)  | 
   582   | 
   611   | 
   583 val r1 = STAR("hello") | 
   612 val r1 = STAR("ab") | 
   584 val r2 = STAR("hello") | STAR("world") | 
   613 val r2 = STAR("hello") | STAR("world") | 
   585   | 
   614   | 
   586   | 
   615   | 
   587 implicit def RexpOps (r: Rexp) = new { | 
   616 implicit def RexpOps (r: Rexp) = new { | 
   588   def | (s: Rexp) = ALT(r, s)  | 
   617   def | (s: Rexp) = ALT(r, s)  |