Created
June 29, 2022 04:44
-
-
Save GusGA/2172a7d51194bd533c13f1984d1630a3 to your computer and use it in GitHub Desktop.
Memoize function in go
This file contains 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
func Memoize(fn func(int) int) func(int) int { | |
cache := make(map[int]int) | |
return func(n int) int { | |
if v, ok := cache[n]; ok { | |
return v | |
} | |
cache[n] = fn(n) | |
return cache[n] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment