Skip to content

Instantly share code, notes, and snippets.

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 vincent-pradeilles/27d9da91b6a5425de5d94328946b2024 to your computer and use it in GitHub Desktop.
Save vincent-pradeilles/27d9da91b6a5425de5d94328946b2024 to your computer and use it in GitHub Desktop.
func cached<In: Hashable, Out>(_ computation: @escaping ((In) -> Out, In) -> Out) -> (In) -> Out {
var cache = [In: Out]()
func wrap(_ input: In) -> Out {
if let cachedValue = cache[input] {
return cachedValue
} else {
let result = computation(wrap, input)
cache[input] = result
return result
}
}
return wrap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment