Skip to content

Instantly share code, notes, and snippets.

@maximveksler
Last active January 2, 2017 13:11
Show Gist options
  • Save maximveksler/755fea21e2c9b7c9fd8d3a2e92532d81 to your computer and use it in GitHub Desktop.
Save maximveksler/755fea21e2c9b7c9fd8d3a2e92532d81 to your computer and use it in GitHub Desktop.
func memoize<T: Hashable, U>(work: @escaping (T)->U) -> (T)->U {
var memo = Dictionary<T, U>()
return { x in
if let q = memo[x] { return q }
let r = work(x)
memo[x] = r
return r
}
}
let itos = memoize {
(n: Int) in
String(n)
}
@maximveksler
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment