Hey! I know Kotlin has lazy properties with its de...
# getting-started
b
Hey! I know Kotlin has lazy properties with its delegates feature. Is there any way to add lazy evaluation (or, equivalently for my use case, memoization) such that the first time I call
foo(arg)
,
foo(arg)
gets calculated, but the second, third etc. times that I call
foo
after that,
foo(arg)
just returns the already computed, stored value. E.g. in Python, this exists in the form of the
@Memoize
operator, if anyone is familiar with that. Is there any way to achieve this in Kotlin or should I just implement the memoization mechanism myself.
j
There is no built-in feature for this. However, you could roll your own higher-order function to support it yourself. Something like
cached(key, args) { ... }
Doing this requires asking yourself lots of questions, though. Like how to synchronize things between threads, how to clean up the cache, etc.
Different use cases call for different solutions
y
Arrow has
MemoizedDeepRecursiveFunction
I believe, but yes you can just as well do your own higher-order function
🤔 1
b
Thanks for the replies! I've heard of Arrow from a thread talking about how its
Either<U,V>
behaves a lot like Rust's
Result<T,E>
enum or like TypeScript's union types; so Youssef, you've made me all the more curious about checking it out. Thanks, guys!
j
@Youssef Shoaib [MOD] how does arrow's cache eviction work? Does everything stay in memory forever? Are calls to this function thread-safe?
y
There's some basic built-in options, but also an integration module with cache-4k, which has all the cool cache eviction stuff you want. See https://arrow-kt.io/learn/collections-functions/recursive/
thank you color 2
j
Short sample of doing it yourself (which is honestly short enough to perhaps not worry about a shortcut):
Copy code
private val results = HashMap<Int, Int>()
fun memoizedFib(number: Int): Int = if(number <= 1) number else results.getOrPut(number) {
    memoizedFib(number - 1) + memoizedFib(number - 2)
}

memoizedFib(11)
Note this will have concurrency issues unless you use
ConcurrentHashMap
instead.
1