Last active
January 2, 2017 13:11
-
-
Save maximveksler/755fea21e2c9b7c9fd8d3a2e92532d81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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<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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See related post https://medium.com/@mvxlr/swift-memoize-walk-through-c5224a558194#.g1fowcm1m