Skip to content

Instantly share code, notes, and snippets.

@yicone
Created January 19, 2017 07:34
Show Gist options
  • Save yicone/d5ad979297cfa7e4e23ffc67b13b515a to your computer and use it in GitHub Desktop.
Save yicone/d5ad979297cfa7e4e23ffc67b13b515a to your computer and use it in GitHub Desktop.
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())
}
}
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