Last active
May 8, 2016 01:08
-
-
Save rustamli/0b06de4ec79f72be855d84dd3e5b876a to your computer and use it in GitHub Desktop.
Fibo Closure Function 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
package main | |
import "fmt" | |
func fibonacci() func() int { | |
beforeLast, last := 0, -1 | |
return func () int { | |
if last < 1 { | |
last += 1 | |
} else { | |
newLast := beforeLast + last | |
beforeLast = last | |
last = newLast | |
} | |
return last | |
} | |
} | |
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