Created
February 14, 2018 14:14
-
-
Save c-yan/4f903ed3ef6bfe28522a84daf88679f0 to your computer and use it in GitHub Desktop.
memoize in go
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" | |
"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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
execution result