Last active
March 2, 2018 08:53
-
-
Save JorgeCastilloPrz/095527d3f97b14fbbf6065645b3295b9 to your computer and use it in GitHub Desktop.
kotlinmemoize for a friend
This file contains hidden or 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
class Memoize1<in T, out R>(val f: (T) -> R) : (T) -> R { | |
private val values = mutableMapOf<T, R>() | |
override fun invoke(x: T): R { | |
return values.getOrPut(x, { f(x) }) | |
} | |
} | |
fun <T, R> ((T) -> R).memoize(): (T) -> R = Memoize1(this) | |
val memoizedSumFactors = { x: Int -> sumOfFactors(x) }.memoize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment