Predict the output
How many times is map called?
fun main() {
var calls = 0
val first = (1..1_000).asSequence()
.map { calls++; it * 2 }
.first { it > 10 }
println("$first $calls")
}Hint 1 of 1
Does map run for every element before first checks?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 12 6
Correct. Elements go one at a time; map runs for 1 to 6, and 6*2 = 12 is the first above 10.
Why not the others?
- B 12 1000. That's what a List chain would do: map everything first.
- C 11 6. map doubles, so values are even.
- D 2 1. 2 isn't greater than 10.