0
0
Kotlinprogramming~10 mins

Lazy evaluation vs eager evaluation in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Lazy evaluation vs eager evaluation
Start
Define Collection
Eager Evaluation
Process All Items Immediately
Result Ready
Lazy Evaluation
Process Items When Needed
Result Computed On Demand
This flow shows how eager evaluation processes all data right away, while lazy evaluation waits and processes data only when needed.
Execution Sample
Kotlin
val numbers = listOf(1, 2, 3, 4)
val eager = numbers.map { println("Eager: $it"); it * 2 }
val lazy = numbers.asSequence().map { println("Lazy: $it"); it * 2 }
lazy.toList()
This code compares eager and lazy evaluation by printing when each item is processed.
Execution Table
StepExpressionActionOutputNotes
1val numbers = listOf(1, 2, 3, 4)Create listNo outputList created eagerly
2val eager = numbers.map { println("Eager: $it"); it * 2 }Map over list eagerlyEager: 1 Eager: 2 Eager: 3 Eager: 4All items processed immediately
3val lazy = numbers.asSequence().map { println("Lazy: $it"); it * 2 }Create lazy sequenceNo outputNo processing yet
4lazy.toList()Trigger processing of lazy sequenceLazy: 1 Lazy: 2 Lazy: 3 Lazy: 4Processing happens now
5EndProgram endsNo outputAll lazy items processed on demand
💡 Execution stops after lazy sequence is converted to list, triggering all processing.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
numbers[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
eagerundefined[2, 4, 6, 8][2, 4, 6, 8][2, 4, 6, 8][2, 4, 6, 8]
lazyundefinedundefinedSequence object[2, 4, 6, 8][2, 4, 6, 8]
Key Moments - 2 Insights
Why does eager evaluation print all items immediately at step 2, but lazy evaluation does not print anything at step 3?
At step 2, map is called on a list, so all items are processed right away, causing prints. At step 3, asSequence creates a lazy sequence that defers processing until needed, so no prints happen yet.
When does the lazy sequence actually process items and print output?
At step 4, calling toList() on the lazy sequence triggers processing of all items, causing the print statements to run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
ANo output
BEager: 1\nEager: 2\nEager: 3\nEager: 4
CLazy: 1\nLazy: 2\nLazy: 3\nLazy: 4
DEager: 1 only
💡 Hint
Check the 'Output' column for step 2 in the execution_table.
At which step does the lazy evaluation start processing items?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look for when 'Lazy:' print statements appear in the output column.
If we remove lazy.toList(), what happens to the lazy sequence processing?
AIt processes immediately at step 3
BIt processes at step 2
CIt never processes items
DIt processes at program end automatically
💡 Hint
Refer to variable_tracker and execution_table step 3 and 4 about when processing happens.
Concept Snapshot
Lazy evaluation delays processing until needed.
Eager evaluation processes immediately.
In Kotlin, sequences are lazy.
List operations are eager.
Use lazy for efficiency with large data.
Call terminal operation to trigger lazy processing.
Full Transcript
This example shows the difference between lazy and eager evaluation in Kotlin. We start with a list of numbers. When we map over the list eagerly, all items are processed and printed immediately. When we create a lazy sequence and map over it, no processing happens until we convert it to a list. This triggers the lazy processing and prints the items. Variables track the state of collections and sequences. Key moments clarify why prints happen at different steps. The quiz tests understanding of when processing occurs and the difference between lazy and eager.