Skip to content

Instantly share code, notes, and snippets.

@GusGA
Created June 29, 2022 04:44
Show Gist options
  • Save GusGA/2172a7d51194bd533c13f1984d1630a3 to your computer and use it in GitHub Desktop.
Save GusGA/2172a7d51194bd533c13f1984d1630a3 to your computer and use it in GitHub Desktop.
Memoize function in go
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