Hello. What is idiomatic way to merge two flows in...
# coroutines
n
Hello. What is idiomatic way to merge two flows in precise positions. Assume following code:
Copy code
val flowA = (1..10).asFlow()
val flowB = flowOf("a","b", "c", "d", "e")
Result I want to get is:
1, 2, a, 3, 4, b, 5, 6, c, 7, 8, d, 9, 10, e
My current solution is to use ReceiveChannel, but it does not look very idiomatic:
Copy code
launch {
  val channelB = flowB.produceIn(this)
  flowA.withIndex().transform { item -> 
    emit(item)
    if (item.index > 0 && item.index % 2 == 1)
      emit(flowB.receive())
  }.collect { /* something useful */ }
}
n
Merges the given flows into a single flow without preserving an order of elements.
It does not meet my requirement of precise positions