From b53879a0fb4fb94f01209c5acb0795d2f63598e4 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 1 Sep 2012 22:33:20 -0400 Subject: [PATCH] Doing the fibonacci closure example but I'm too sleepy to figure out the right way to make the first two yielded values 1's. --- gotime/src/exercise-fibonacci-closure.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 gotime/src/exercise-fibonacci-closure.go diff --git a/gotime/src/exercise-fibonacci-closure.go b/gotime/src/exercise-fibonacci-closure.go new file mode 100644 index 0000000..2ab954b --- /dev/null +++ b/gotime/src/exercise-fibonacci-closure.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +// fibonacci is a function that returns +// a function that returns an int. +func fibonacci() func() int { + prev := 0 + cur := 1 + return func() int { + old_cur := cur + cur = prev + cur + prev = old_cur + return cur + } +} + +func main() { + f := fibonacci() + for i := 0; i < 10; i++ { + fmt.Println(f()) + } +}