memoize
Memoizes the given pure function so that invocations with the same arguments will only execute the function once.
import arrow.core.memoize
fun someWorkIntensiveFunction(someParam: Int): String = "$someParam"
fun main() {
//sampleStart
val memoizedF = ::someWorkIntensiveFunction.memoize()
// The first invocation will store the argument and result in a cache inside the `memoizedF` reference.
val value1 = memoizedF(42)
// This second invocation won't really call the `someWorkIntensiveFunction` function
//but retrieve the result from the previous invocation instead.
val value2 = memoizedF(42)
//sampleEnd
println("$value1 $value2")
}
Content copied to clipboard
Note that calling this function with the same parameters in parallel might cause the function to be executed twice.