atMostOnce

inline fun <A> atMostOnce(crossinline f: () -> A): Eval.Later<A>

Creates an Eval instance from a function deferring it's evaluation until .value() is invoked memoizing the computed value.

⚠️ This function ensures that the value is initialized at most once, even in concurrent scenarios. As a result, the thread or coroutine may block or suspend to guarantee that invariant.

Parameters

f

is a function or computation that will be called only once when .value() is invoked for the first time.

import arrow.eval.*

fun main() {
//sampleStart
val lazyEvaled = Eval.atMostOnce { "expensive computation" }
println(lazyEvaled.value())
//sampleEnd
}

"expensive computation" is only computed once since the results are memoized and multiple calls to value() will just return the cached value.