Skip to content

Instantly share code, notes, and snippets.

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