- 
      
- 
        Save julianshen/3938201 to your computer and use it in GitHub Desktop. 
    [Go] fibonacci with closure
  
        
  
    
      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() uint64 { | |
| var x, y, z uint64 = 0, 1, 0 | |
| return func() uint64 { | |
| z, x, y = x, y, x+y | |
| return z | |
| } | |
| } | |
| func main() { | |
| f := fibonacci() | |
| for i := 0; i < 10; i++ { | |
| fmt.Println(f()) | |
| } | |
| } | 
  
    
      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
    
  
  
    
  | function fibonacci() { | |
| var x = 0; | |
| var y = 1; | |
| var z = 0; | |
| return function() { | |
| z = x; | |
| x = y; | |
| y = y + z; | |
| return z; | |
| } | |
| } | |
| var f=fibonacci(); | |
| for(i=0;i<10;i++) { | |
| console.log(f()); | |
| } | 
  
    
      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
    
  
  
    
  | hypot := func(x, y float64) float64 { | |
| return math.Sqrt(x*x + y*y) | |
| } | |
| func adder() func(int) int { | |
| sum := 0 | |
| return func(x int) int { | |
| sum += x | |
| return sum | |
| } | |
| } | |
  
    
      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(n int) { | |
| var x, y, z uint64 = 0, 1, 0 | |
| for i:=0;i<n;i++ { | |
| z = x; | |
| x = y; | |
| y = y + z; | |
| fmt.Println(z); | |
| } | |
| } | |
| func main() { | |
| fibonacci(10) | |
| } | 
  
    
      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" | |
| var m map[int]uint64 | |
| // fibonacci is a function that returns | |
| // a function that returns an int. | |
| func fibonacci(n int) uint64 { | |
| var x uint64 | |
| v, ok := m[n] | |
| if ok { | |
| return v | |
| } | |
| switch { | |
| case n<= 1: | |
| x = uint64(n) | |
| default: | |
| x = fibonacci(n-1) + fibonacci(n -2) | |
| } | |
| m[n] = x | |
| return x | |
| } | |
| func main() { | |
| m = make(map[int]uint64) | |
| for i:=0;i<50;i++ { | |
| fmt.Println(fibonacci(i)) | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment