https://kotlinlang.org logo
Title
r

robstoll

05/04/2018, 2:02 PM
does kotlin have a way to turn a function into one which is evaluated only once? Something like
fun <R> (() -> R).evalOnce(): () -> R {
    val v by lazy { this() }
    return { v }
}
but which works also for functions with one or more parameters?
p

poohbar

05/04/2018, 2:03 PM
Do you mean memoization?
r

robstoll

05/04/2018, 2:03 PM
kind of yes
j

jlleitschuh

05/04/2018, 2:04 PM
Well part of the memoization would involve making sure that the parameters matched a previous call. You'd probably have to roll your own on this one.
s

spand

05/04/2018, 2:05 PM
It seems you want a cache
👍 1
r

robstoll

05/04/2018, 2:05 PM
ok, thanks for the information
j

jlleitschuh

05/04/2018, 2:53 PM
If you want to make this generic, I'd suggest that you make your memoization function, a simple way to get around the multi-argument problem is just make a version of the function that takes one arg and when you need more args, throw those into a data class.