Originally published at
Moishe Beshkin. You can comment here or
there.
Exercise: Fibonacci closure package main
import "fmt"
var prev, curr int
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
inc := 1
return func() int {
prev = curr
curr = inc
inc = prev+curr
return prev
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}