Advertisement
Omnikron13

Fibonacci Closure

Aug 13th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.36 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. // fibonacci is a function that returns
  6. // a function that returns an int.
  7. func fibonacci() func() int {
  8.     a, b := 1, 0
  9.     return func() int {
  10.         c := b
  11.         b = a + b
  12.         a = c
  13.         return b
  14.     }
  15. }
  16.  
  17. func main() {
  18.     f := fibonacci()
  19.     for i := 0; i < 10; i++ {
  20.         fmt.Println(f())
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement