From 185391fb22028c44a25ef9b61fea1bcc5c13ab2d Mon Sep 17 00:00:00 2001 From: Ola Bini Date: Thu, 5 Nov 2009 17:48:51 +0100 Subject: [PATCH] Add a fib example --- examples/misc/lastFib.ik | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/misc/lastFib.ik diff --git a/examples/misc/lastFib.ik b/examples/misc/lastFib.ik new file mode 100644 index 00000000..67900987 --- /dev/null +++ b/examples/misc/lastFib.ik @@ -0,0 +1,30 @@ + +FibSequence = Sequence with( + initialize: method( + @index = 0 + @last1 = 1 + @last2 = 1 + ), + + next?: true, + next: method( + result = case(@index, + 0, 1, + 1, 1, + else, @last1 + @last2) + + @index++ + @last1 = @last2 + @last2 = result + + result + ) +) + +fib = method("Returns a sequence that generates all the fibonacci numbers", + FibSequence mimic +) + +; find the index of the first fibonacci number larger than a thousand +(fib indexed(from: 1) takeWhile(second < 1000) last first + 1) println +fib indexed(from: 1) droppedWhile(second < 1000) first first println