Skip to content

Instantly share code, notes, and snippets.

@roschlau
Created September 21, 2017 13:09
Show Gist options
  • Save roschlau/1f9fbb8966338e1c4d024a5522c2cee3 to your computer and use it in GitHub Desktop.
Save roschlau/1f9fbb8966338e1c4d024a5522c2cee3 to your computer and use it in GitHub Desktop.
Simple function to memoize function calls
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