Does Flow have something like RxJava's `compose` ...
# coroutines
j
Does Flow have something like RxJava's
compose
operator? (Rather than operating individual items emitted by a Flow, it would operate on the Flow itself, returning a new composed Flow.)
o
no, it's basically implemented by normal composition
👍🏾 1
e.g. if you wanted a custom `mapAndFilter`:
Copy code
fun <T, V> Flow<T>.mapAndFilter(map: suspend (T) -> V, filter: suspend (V) -> Boolean): Flow<V> {
  return map(map).filter(filter)
}
or custom logic:
Copy code
fun Flow<Int>.addThreeToEveryThird(): Flow<Int> {
  val original = this
  return flow {
     var i = 0
     original.collect {
       i++
       when (i % 3) {
         0 -> emit(it + 3)
         else -> emit(it)
       }
     }
  }
}
j
Yup! You beat me to it, was just about to write: Ok. I see how I could use an extension function on
Flow
that could return a transformed
Flow
.
Thanks @octylFractal!
👍 1
l
Are you looking for the
combine
operator? This one exists as a top level function, and an extension of
Flow<*>
.
o
No, RxJava
compose
is basically as described above -- it allows the mapping of a
Flow<T>
to
Flow<V>
as a whole, not based on intermediate elements. It is also not a merging device.
👍🏾 1