Created
August 27, 2014 06:21
-
-
Save kamatama41/ec96ceb09772c2eefc62 to your computer and use it in GitHub Desktop.
My answer of "a tour of Go #44( http://go-tour-jp.appspot.com/#44 )"
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 is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
now, next := 1, 0 | |
return func() int { | |
new_now, new_next := next, now+next | |
now, next = new_now, new_next | |
return next | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} | |
// result | |
//1 | |
//1 | |
//2 | |
//3 | |
//5 | |
//8 | |
//13 | |
//21 | |
//34 | |
//55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment