Created
September 21, 2017 13:09
-
-
Save roschlau/1f9fbb8966338e1c4d024a5522c2cee3 to your computer and use it in GitHub Desktop.
Simple function to memoize function calls
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
fun <T, U> memoized(function: (T) -> U): (T) -> U { | |
val cache = mutableMapOf<T, U>() | |
return { t -> cache.getOrPut(t) { function(t) } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment