300 val list = (1 to 1000000).toList  | 
   300 val list = (1 to 1000000).toList  | 
   301 time_needed(10, for (n <- list) yield n + 42)  | 
   301 time_needed(10, for (n <- list) yield n + 42)  | 
   302 time_needed(10, for (n <- list.par) yield n + 42)  | 
   302 time_needed(10, for (n <- list.par) yield n + 42)  | 
   303   | 
   303   | 
   304   | 
   304   | 
   305   | 
   305 // mutable vs immutable factorial  | 
   306   | 
   306 def fact_mu(n: Long): Long = { | 
   307   | 
   307   var result : Long = 1  | 
         | 
   308   var i = 1  | 
         | 
   309   while (i <= n) { | 
         | 
   310     result = result * i  | 
         | 
   311     i = i + 1  | 
         | 
   312   }  | 
         | 
   313   result  | 
         | 
   314 }  | 
         | 
   315   | 
         | 
   316 def fact_im(num: Long): Long = { | 
         | 
   317   if (num == 1) num else  | 
         | 
   318     num * fact_im(num - 1)  | 
         | 
   319 }  | 
         | 
   320   | 
         | 
   321 def test() = { | 
         | 
   322   for (i <- (0 to 10).par) yield { | 
         | 
   323     val l1 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_mu(n)  | 
         | 
   324     val l2 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_im(n)   | 
         | 
   325     l1.sum - l2.sum  | 
         | 
   326   }  | 
         | 
   327 }  | 
         | 
   328   | 
         | 
   329 test().sum  | 
         | 
   330 println(l1.sum - l2.sum)  | 
   308   | 
   331   | 
   309 // Webpages  | 
   332 // Webpages  | 
   310 //==========  | 
   333 //==========  | 
   311   | 
   334   | 
   312 import io.Source  | 
   335 import io.Source  | 
   454 // There are also a massive number of Scala tutorials on youtube  | 
   477 // There are also a massive number of Scala tutorials on youtube  | 
   455 // and there are tons of books and free material.  | 
   478 // and there are tons of books and free material.  | 
   456 //  | 
   479 //  | 
   457   | 
   480   | 
   458   | 
   481   | 
         | 
   482   | 
         | 
   483   | 
         | 
   484   | 
         | 
   485   | 
         | 
   486   | 
         | 
   487   | 
         | 
   488   | 
         | 
   489 // advantage of case classes  | 
         | 
   490 //  | 
         | 
   491 case class Participant(name: String, score: Int, active: Boolean)  | 
         | 
   492   | 
         | 
   493 val ps = Seq(Participant("Jack", 34, true), | 
         | 
   494              Participant("Tom", 51, true), | 
         | 
   495              Participant("Bob", 90, false)) | 
         | 
   496                | 
         | 
   497 ps.filter(_.score < 50).  | 
         | 
   498    filter(_.active).  | 
         | 
   499    map(_.copy(active = false))  | 
         | 
   500   | 
         | 
   501   | 
         | 
   502   | 
         | 
   503 // another example why state is bad...does not work yet  | 
         | 
   504   | 
         | 
   505 // for measuring time  | 
         | 
   506 def time_needed[T](n: Int, code: => T) = { | 
         | 
   507   val start = System.nanoTime()  | 
         | 
   508   for (i <- (0 to n)) code  | 
         | 
   509   val end = System.nanoTime()  | 
         | 
   510   (end - start) / 1.0e9  | 
         | 
   511 }  | 
         | 
   512   | 
         | 
   513 def santa_state(plan: List[Char]) : Int = {   | 
         | 
   514   | 
         | 
   515   var floor = 0  | 
         | 
   516   | 
         | 
   517   for (i <- plan.par) { | 
         | 
   518     if (i == '(') { | 
         | 
   519       floor += 1  | 
         | 
   520     } else { | 
         | 
   521       floor -= 1  | 
         | 
   522     }  | 
         | 
   523   }  | 
         | 
   524     | 
         | 
   525   floor  | 
         | 
   526 }  | 
         | 
   527   | 
         | 
   528 def i(c: Char) = if (c == '(') 1 else -1 | 
         | 
   529   | 
         | 
   530 def santa_imutable(plan: List[Char]) : Int =  | 
         | 
   531   plan.par.map(i(_)).reduce(_ + _)  | 
         | 
   532   | 
         | 
   533 santa_state("(((((()))))".toList) | 
         | 
   534 santa_imutable("(((((()))))".toList) | 
         | 
   535   | 
         | 
   536 def randomString(length: Int) =   | 
         | 
   537   List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)  | 
         | 
   538   | 
         | 
   539   | 
         | 
   540 santa_state(randomString(100))  | 
         | 
   541   | 
         | 
   542 val large_string = randomString(3000000)  | 
         | 
   543   | 
         | 
   544 time_needed(10, santa_state(large_string))  | 
         | 
   545 time_needed(10, santa_imutable(large_string))  |