Guest User

Untitled

a guest
May 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 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. n := 0
  9. history := []int{0, 1}
  10. return func() int {
  11. defer func() { n += 1 }()
  12.  
  13. if n < 2 {
  14. return history[n]
  15. } else {
  16. ret := history[0] + history[1]
  17. history[0] = history[1]
  18. history[1] = ret
  19. return ret
  20. }
  21. }
  22. }
  23.  
  24. func main() {
  25. f := fibonacci()
  26. for i := 0; i < 10; i++ {
  27. fmt.Println(f())
  28. }
  29. }
Add Comment
Please, Sign In to add comment