Last active
August 29, 2015 13:56
-
-
Save sighmin/9175834 to your computer and use it in GitHub Desktop.
Go tour "Fibonacci closure factory" method: http://tour.golang.org
This file contains hidden or 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
package main | |
import "fmt" | |
// Fibonacci closure factory | |
func fibonacci() func() int { | |
previous := 0 | |
current := 1 | |
return func() int { | |
result := previous | |
previous = current | |
current = result + current | |
return result | |
} | |
} | |
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