Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- // fibonacci is a function that returns
- // a function that returns an int.
- func fibonacci() func() int {
- n := 0
- history := []int{0, 1}
- return func() int {
- defer func() { n += 1 }()
- if n < 2 {
- return history[n]
- } else {
- ret := history[0] + history[1]
- history[0] = history[1]
- history[1] = ret
- return ret
- }
- }
- }
- func main() {
- f := fibonacci()
- for i := 0; i < 10; i++ {
- fmt.Println(f())
- }
- }
Add Comment
Please, Sign In to add comment