Predict the output
Values arrive every 100 ms; each takes 250 ms to handle. What does conflate print?
fun main() = runBlocking {
flow {
for (i in 1..5) { delay(100); emit(i) }
}
.conflate()
.collect { delay(250); print("$it ") }
}Hint 1 of 1
Which values arrive while 1 is being handled (100–350 ms)?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 1 3 5
Correct. While 1 is handled (100–350 ms), 2 and 3 arrive and only 3 is kept; while 3 is handled (350–600 ms), 4 and 5 arrive and 5 is kept.
Why not the others?
- B 1 2 3 4 5. That's the default or buffer(): every value is handled.
- C 5. The first value is handled straight away.
- D 1 4 5. Conflate keeps the latest value when the collector is free; at 350 ms that is 3, not 4.
Text version
| Version | What runs when | Finishes at |
|---|---|---|
| Default (sequential) |
| t = 1.75 s |
| conflate() |
| t = 0.85 s |