Commits

Miki Tebeka committed 4763b24

takewhile

Comments (0)

Files changed (1)

 #
 # Answer: 4613732
 
-function fibs(upto)
-    function inner(upto)
+function fibs()
+    function inner()
         a, b = 1, 2
-        while a <= upto
+        while true
             produce(a)
             a, b = b, a + b
         end
     end
-    return Task(() -> inner(upto))
+    return @task inner()
 end
 
-println(sum(filter(iseven, collect(fibs(4000000)))))
+function takewhile(pred, iter)
+    function inner()
+        for val = iter
+            if !pred(val)
+                break
+            end
+            produce(val)
+        end
+    end
+
+    return @task inner()
+end
+
+println(sum(filter(iseven, collect(takewhile(n -> n < 4000000, fibs())))))