Created
January 19, 2017 07:33
-
-
Save yicone/7295fbf63062ea4580dac24abc4b0c5d to your computer and use it in GitHub Desktop.
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" | |
// fibonacci 函数会返回一个返回 int 的函数。 | |
func fibonacci() func() int { | |
n0 := 0 | |
n1 := 1 | |
return func() int { | |
n2 := n0 + n1 | |
n0 = n1 | |
n1 = n2 | |
return n2 | |
} | |
} | |
func main() {func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
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
function fibonacci() { | |
var n0 = 0 | |
var n1 = 1 | |
return function() { | |
n2 = n0 + n1 | |
n0 = n1 | |
n1 = n2 | |
return n2 | |
} | |
} | |
var f = fibonacci() | |
for (var i = 0; i < 10; i++) { | |
console.log(f()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment