Skip to content

Instantly share code, notes, and snippets.

@beelsebob
Forked from jeremytregunna/gist:d9c7572e2dd50c584e7a
Last active August 29, 2015 14:10
Show Gist options
  • Save beelsebob/92b3d3c393fb5222a59f to your computer and use it in GitHub Desktop.
Save beelsebob/92b3d3c393fb5222a59f to your computer and use it in GitHub Desktop.
public func memoize<T: Hashable, U>(f: (T) -> U) -> (T) -> U {
var memo : [T : U] = [:]
return { x in
if let q = memo[x] {
return q
}
let r = f(x)
memo[x] = r
return r
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment