Skip to content

Instantly share code, notes, and snippets.

@tmtk75
Last active December 19, 2015 12:39
Show Gist options
  • Select an option

  • Save tmtk75/5956735 to your computer and use it in GitHub Desktop.

Select an option

Save tmtk75/5956735 to your computer and use it in GitHub Desktop.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a := []int{0, 1, 1}
i := 0
return func() int {
i++
if i < len(a) {return a[i]}
a[1], a[2] = a[2], a[2] + a[1]
return a[2]
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment