Skip to content

Instantly share code, notes, and snippets.

@oludouglas
Created September 15, 2020 12:40
Show Gist options
  • Save oludouglas/4e06d9b3b02a0055abcc5cd938b1d683 to your computer and use it in GitHub Desktop.
Save oludouglas/4e06d9b3b02a0055abcc5cd938b1d683 to your computer and use it in GitHub Desktop.
//fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2)
func fib() func() int {
a, b := 0, 1
return func() int {
defer func() {
a, b = b, a+b
}()
return a
}
}
func main() {
f := fib()
// Function calls are evaluated left-to-right.
for i := 0; i <= 4; i++ {
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment