Last active
July 10, 2019 18:35
-
-
Save vincent-pradeilles/db173183776be2efc4fcd983b4be14b2 to your computer and use it in GitHub Desktop.
This file contains 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 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