Skip to content

Instantly share code, notes, and snippets.

@c-yan
Created February 14, 2018 14:14
Show Gist options
  • Save c-yan/4f903ed3ef6bfe28522a84daf88679f0 to your computer and use it in GitHub Desktop.
Save c-yan/4f903ed3ef6bfe28522a84daf88679f0 to your computer and use it in GitHub Desktop.
memoize in go
package main
import (
"fmt"
"github.com/BenLubar/memoize"
)
func main() {
var factorial func(n int) int
factorial = func(n int) int {
fmt.Printf("call factorial: %d\n", n)
if n == 1 {
return 1
} else {
return n * factorial(n-1)
}
}
factorial = memoize.Memoize(factorial).(func(int) int)
fmt.Println(factorial(4))
fmt.Println(factorial(6))
}
@c-yan
Copy link
Author

c-yan commented Feb 14, 2018

execution result

>memoized-factorial.exe
call factorial: 4
call factorial: 3
call factorial: 2
call factorial: 1
24
call factorial: 6
call factorial: 5
720

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment