progs/lecture2.scala
changeset 167 349d706586ef
parent 155 371acb50643d
child 173 2de1f79dedf0
equal deleted inserted replaced
166:780c40aaad27 167:349d706586ef
   291 
   291 
   292 add2_9_c(10)
   292 add2_9_c(10)
   293 
   293 
   294 sum(add(2), 0, 2)
   294 sum(add(2), 0, 2)
   295 sum(add(10), 0, 2)
   295 sum(add(10), 0, 2)
       
   296 
       
   297 
       
   298 
       
   299 
       
   300 // some automatic timing in each evaluation
       
   301 package wrappers {  
       
   302 
       
   303   object wrap { 
       
   304    
       
   305     def timed[R](block: => R): R = {
       
   306       val t0 = System.nanoTime()
       
   307       val result = block
       
   308       println("Elapsed time: " + (System.nanoTime - t0) + "ns")
       
   309       result
       
   310     }
       
   311 
       
   312     def apply[A](a: => A): A = { 
       
   313       timed(a)
       
   314     } 
       
   315   }
       
   316 }
       
   317 
       
   318 $intp.setExecutionWrapper("wrappers.wrap")
       
   319 
       
   320 // Iteration
       
   321 
       
   322 def fib(n: Int) : Int = 
       
   323   if (n <= 1) 1 else fib(n - 1) + fib(n - 2)
       
   324 
       
   325 fib(10)
       
   326 
       
   327 
       
   328 Iterator.iterate((1,1)){ case (n: Int, m: Int) => (n + m, n) }.drop(9).next
       
   329 
       
   330 
   296 
   331 
   297 
   332 
   298 // Function Composition
   333 // Function Composition
   299 //======================
   334 //======================
   300 
   335