Complete the code to create a sequence from a list.
val numbers = listOf(1, 2, 3, 4, 5) val seq = numbers.[1]()
The asSequence() function converts a collection to a sequence for lazy operations.
Complete the code to get the first element greater than 3 from a sequence.
val seq = sequenceOf(1, 2, 3, 4, 5) val first = seq.filter { it > 3 }.[1]()
The first() function returns the first element matching the condition.
Fix the error in the code to sum numbers lazily using a sequence.
val numbers = listOf(1, 2, 3, 4, 5) val sum = numbers.[1]().map { it * 2 }.sum()
Using asSequence() allows lazy evaluation before summing.
Fill both blanks to create a sequence and filter even numbers.
val numbers = listOf(1, 2, 3, 4, 5) val evens = numbers.[1]().[2] { it % 2 == 0 }.toList()
First convert to sequence with asSequence(), then filter even numbers with filter.
Fill all three blanks to create a sequence, map values, and find the max.
val numbers = listOf(1, 2, 3, 4, 5) val maxVal = numbers.[1]().[2] { it * 3 }.[3]()
Convert to sequence with asSequence(), map each value by multiplying by 3, then find the max with max().
