Complete the code to create a lazy value in Kotlin.
val lazyValue: String by [1] { println("Computed!") "Hello" }
In Kotlin, lazy is used to create a lazily evaluated property.
Complete the code to create an eager list in Kotlin.
val numbers = listOf(1, 2, 3, 4).[1]()
toList() creates a new list eagerly from the original collection.
Fix the error in the code to make the sequence evaluated lazily.
val seq = listOf(1, 2, 3).[1]().map { it * 2 }
asSequence() converts the list to a lazy sequence, delaying computation.
Fill both blanks to create a lazy sequence and then convert it back to a list eagerly.
val result = listOf(1, 2, 3).[1]().map { it + 1 }.[2]()
First, asSequence() creates a lazy sequence, then toList() collects results eagerly.
Fill all three blanks to create a lazy property, access it, and print its value.
val lazyVal: String by [1] { println("Computing...") "Kotlin" } fun main() { println(lazyVal) println(lazyVal.[2]) println(lazyVal.[3]) }
The property is lazy initialized with lazy. Then we print its length and uppercase form.
