Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 08:48
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save tetsuok/2281812 to your computer and use it in GitHub Desktop.
Save tetsuok/2281812 to your computer and use it in GitHub Desktop.
An answer of the exercise: Fibonacci closure on a tour of Go
package main
import "fmt"
// Very naive answer.
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
n := 0
a := 0
b := 1
c := a + b
return func() int {
var ret int
switch {
case n == 0:
n++
ret = 0
case n == 1:
n++
ret = 1
default:
ret = c
a = b
b = c
c = a + b
}
return ret
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
@ydzh
Copy link

ydzh commented Mar 23, 2022

func fibonacci() func() int {
	first := -1
	second := 1
	return func() int {
		first, second = second, first + second
		return second
	}
}

@nerewarin
Copy link

nerewarin commented May 2, 2022

func fibonacci() func() int {
	i := -1
	var fib func(i int) int
	
	fib = func(x int) int {
		switch x {
			case 0:
				return 0
			case 1:
				return 1
			default:
				return fib(x-1) + fib(x-2)
		}
	}
	
	fib2 := func() int {
		i++
		return fib(i)
	}
	return fib2
}

@NanmiCoder
Copy link

func fibonacci() func() int {
	a, b := 0, 1
	fib := func() int {
		var result int
		if a == 0 {
			result = 0
		} else {
			result = b
		}
		a, b = b, a+b
		return result
	}
	return fib
}

@niyazz
Copy link

niyazz commented May 16, 2022

func fibonacci() func() int {
	array := [2]int {-1, 1}
	return func() int{
		res := array[0] + array[1]
		array[0] = array[1]
		array[1] = res
		return res
	}
}

@QuanTT0110
Copy link

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	
	first, second := 0, 1
	
	return (func () int {
		first, second = second, first + second
		return second
		
	})

}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

anybody can explain for me about this line "first, second = second, first + second" ? please, i don't understand

@oboff
Copy link

oboff commented Nov 11, 2022

func fibonacci() func() int {
	a,b,c := 0, 0, 1
	return func() int {
		a = b
		b, c = c, b+c
		return a
	}
}

@oboff
Copy link

oboff commented Nov 11, 2022

@QuanTT0110
similar to writing:

first = second
second = first + second

@hktrib
Copy link

hktrib commented Jul 13, 2023

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	
	first, second := 0, 1
	
	return (func () int {
		first, second = second, first + second
		return second
		
	})

}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

anybody can explain for me about this line "first, second = second, first + second" ? please, i don't understand

I think you need to create a copy. Remember you are using a accumulating reference in first and second. Also think about your logic one more time

@guru-das-s
Copy link

guru-das-s commented Sep 15, 2023

I solved this using a defer function:

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	var a, b int = 0, 1

	return func() int {
		update_fn := func() {
			a, b = b, b+a
		}
		defer update_fn()
		return a
	}
}

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