Predict the output
What does this print?
fun main() = runBlocking {
val letters = flowOf("a", "b", "c")
val numbers = flowOf(1, 2)
letters.zip(numbers) { l, n -> "$l$n" }
.collect { print("$it ") }
}Hint 1 of 2
Does zip ever reuse a value?
Hint 2 of 2
What happens when one flow runs out?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A a1 b2
Correct. zip pairs items one-to-one and stops when the shorter flow completes.
Why not the others?
- B a1 b2 c2. That's combine-like behaviour with the latest value; zip never reuses a value.
- C a1 b1 b2 c2. That's what combine can produce when sources emit at different times.
- D a1 b2 c. zip needs a value from both flows for every item.