Created
February 14, 2018 15:09
-
-
Save c-yan/d8e64ffab357555db66091a9d17d9c89 to your computer and use it in GitHub Desktop.
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" | |
| ) | |
| func main() { | |
| memoizedFactorial := func() func(int) int { | |
| cache := make(map[int]int) | |
| var factorial func(n int) int | |
| factorial = func(n int) int { | |
| v, ok := cache[n] | |
| if ok { | |
| return v | |
| } | |
| fmt.Printf("call factorial: %d\n", n) | |
| if n == 1 { | |
| return 1 | |
| } else { | |
| cache[n] = n * factorial(n-1) | |
| return cache[n] | |
| } | |
| } | |
| return factorial | |
| } | |
| factorial := memoizedFactorial() | |
| 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