Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created June 15, 2015 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/7b1e3568b671e512fbf0 to your computer and use it in GitHub Desktop.
Save oisdk/7b1e3568b671e512fbf0 to your computer and use it in GitHub Desktop.
func memoize<A : Hashable, T>(var base: [A:T], recFunc: ((A -> T), A) -> T) -> (A -> T) {
func f(n: A) -> T {
if let r = base[n] { return r }
let r = recFunc(f, n)
base[n] = r
return r
}
return f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment