progs/lecture2.scala
changeset 167 349d706586ef
parent 155 371acb50643d
child 173 2de1f79dedf0
--- a/progs/lecture2.scala	Sat Dec 16 23:53:28 2017 +0000
+++ b/progs/lecture2.scala	Mon Jan 15 23:15:34 2018 +0000
@@ -295,6 +295,41 @@
 sum(add(10), 0, 2)
 
 
+
+
+// some automatic timing in each evaluation
+package wrappers {  
+
+  object wrap { 
+   
+    def timed[R](block: => R): R = {
+      val t0 = System.nanoTime()
+      val result = block
+      println("Elapsed time: " + (System.nanoTime - t0) + "ns")
+      result
+    }
+
+    def apply[A](a: => A): A = { 
+      timed(a)
+    } 
+  }
+}
+
+$intp.setExecutionWrapper("wrappers.wrap")
+
+// Iteration
+
+def fib(n: Int) : Int = 
+  if (n <= 1) 1 else fib(n - 1) + fib(n - 2)
+
+fib(10)
+
+
+Iterator.iterate((1,1)){ case (n: Int, m: Int) => (n + m, n) }.drop(9).next
+
+
+
+
 // Function Composition
 //======================