Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created November 26, 2014 18:17
Show Gist options
  • Save jeremytregunna/d9c7572e2dd50c584e7a to your computer and use it in GitHub Desktop.
Save jeremytregunna/d9c7572e2dd50c584e7a to your computer and use it in GitHub Desktop.
public func memoize<T: Hashable, U>(body: ((T) -> U, T) -> U) -> (T) -> U {
var memo = Dictionary<T, U>()
var result: ((T) -> U)!
result = { x in
if let q = memo[x] {
return q
}
let r = body(result, x)
memo[x] = r
return r
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment