Created
January 6, 2014 02:11
-
-
Save npat-efault/8277123 to your computer and use it in GitHub Desktop.
Fibonacci iterator in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fibonacci series iterator using closure | |
package main | |
import "fmt" | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x, y = y, x + y | |
return x | |
} | |
} | |
func main() { | |
fib := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(fib()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment