concept lazy in category functional programming

appears as: lazy
Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

The notion of non-strict or lazy evaluation comes to the rescue here—the value is only computed at the point where it is actually referenced, not where it is declared. We are no longer greedy in performing all the calculations, but instead compute them on demand.

This language feature is implemented in Kotlin by way of a builtin function called lazy. An instance of Lazy<T> (with T being the type of the value to be assigned) is returned when this function is invoked with a lambda thunk as argument. This Lazy object serves as delegate for implementing a lazy property. Delegation is expressed by using the by keyword.

val x: Int by lazy { expensiveOp() } #1

fun useit() =
    if (x > 10) "hi" #2
    else if (x == 0) "zero" #3
    else ("lo")

The lazy property x is initialized on first access by executing expensiveOp inside the lambda thunk that was passed into the lazy function. The result is then cached inside the delegate object for subsequent evaluations to take advantage of.