Created
January 9, 2012 04:10
-
-
Save ryochack/1581019 to your computer and use it in GitHub Desktop.
"A Tour of Go" http://tour.golang.org/#47
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
/* | |
* http://tour.golang.org/#47 | |
* OR | |
* http://http://go-tour-jp.appspot.com/#46 | |
*/ | |
package main | |
import ( | |
"fmt" | |
) | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
n, ok := 0, false | |
a, b := 0, 0 | |
return func() int { | |
var ret int = a + b | |
if false != ok { | |
if a > b { | |
b += a | |
ret = b | |
} else { | |
a += b | |
ret = a | |
} | |
} else { | |
ret = n | |
n++ | |
if 1 < n { | |
ok = true | |
a = 1 | |
} | |
} | |
return ret | |
} | |
} | |
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