question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

combineLatest(Iterable<Flow>) operator

See original GitHub issue

Please add a combineLatest operator which accepts a list of Flows. Here is an example of my use case:

fun <T> combineLatest(flows: Iterable<Flow<T>>): Flow<List<T>> = TODO()

// A list of connected devices.
val devices: List<Device> = getDevices()

// A list of Flows which receive the state of the device every time it changes.
val flows: List<Flow<DeviceState>> = devices.map { device ->
    device.observeState() // Flow<DeviceState>
}

// Combine the latest state of the devices into a single Flow.
val flow: Flow<List<DeviceState>> = combineLatest(flows)


// Somewhere else in the code
flow.collect { states ->
    for (state in states) {
        // TODO: do something with the states.
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
PaulWoitaschekcommented, Jun 27, 2019

I find flow.combineLatest(other) only useful for that single overload. If there are more than 2 sources I do strongly prefer a factory method.

For example if we take this one: https://github.com/Kotlin/kotlinx.coroutines/blob/f77533cb7840afd0afa5fa4a97da6fd22f84f975/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt#L120-L134

This makes no sense for me. It’s not that I want to combine source A with the latest of B,C,D,E. Instead all these sources are from the user perspective equally important. If we look at this signature, it implies as if the receiver flow is different from the flows passed as arguments.

3reactions
ZakTaccardicommented, Jul 23, 2019

Just to add on, I find the first example below easier to read than the second.

combineLatest(
  flow1,
  flow2,
  flow3
) { .. }
flow1.combineLatest(
  flow2,
  flow3
) { .. }

flow1 has no additional significance over flow2, so I find seeing them written in parallel easier to read because they emit in parallel

Read more comments on GitHub >

github_iconTop Results From Across the Web

combine - Kotlin
Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow. Sources. common source....
Read more >
CombineLatest operator - ReactiveX
RxPHP implements this operator as combineLatest . Merges the specified observable sequences into one observable sequence by using the selector function whenever ...
Read more >
Flowable (RxJava Javadoc 2.1.10)
The Flowable class that implements the Reactive-Streams Pattern and offers factory methods, intermediate operators and the ability to consume reactive ...
Read more >
Flux (reactor-core 3.5.1)
Concatenate all sources provided in an Iterable , forwarding elements emitted by the sources downstream. Concatenate all sources emitted as an onNext signal ......
Read more >
An Early look at Kotlin Coroutine's Flow - ProAndroidDev
asFlow()Iterable<T>. ... Flow is not currently as operator rich as RxJava but as at when this post was written, there are 27 operators...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found