mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 06:16:03 +03:00
39 lines
533 B
Go
39 lines
533 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type Memoized func(int) int
|
|
|
|
var fibMem = Memoize(fib)
|
|
|
|
func Memoize(mf Memoized) Memoized {
|
|
cache := make(map[int]int)
|
|
return func(key int) int {
|
|
if val, found := cache[key]; found {
|
|
return val
|
|
}
|
|
temp := mf(key)
|
|
cache[key] = temp
|
|
return temp
|
|
}
|
|
}
|
|
|
|
func FibMemoized(n int) int {
|
|
return fibMem(n)
|
|
}
|
|
|
|
func fib(x int) int {
|
|
if x == 0 {
|
|
return 0
|
|
} else if x <= 2 {
|
|
return 1
|
|
} else {
|
|
return fib(x-2) + fib(x-1)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(fib(30))
|
|
fmt.Println(FibMemoized(30))
|
|
}
|