https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Thomas

04/21/2019, 4:55 PM
Is there an example somewhere how to implement switchMap for Kotlin Flows?
l

louiscad

04/21/2019, 4:56 PM
Isn't it called
flatMap
?
s

simon.vergauwen

04/21/2019, 5:00 PM
ReactiveX offers different strategies on how to interleave resulting streams.
Note that FlatMap merges the emissions of these Observables, so that they may interleave.
These docs makes comparison with other operators. http://reactivex.io/documentation/operators/flatmap.html
flatMapConcat
or
flatMapMerge
look like good examples.
t

Thomas

04/21/2019, 6:25 PM
@simon.vergauwen thanks for the link, the comparison looks very interesting. I am currently using the switchMap version. Would that mean I need to make my own implementation or is there something (similar) already available in Flow?
@louiscad
flatMap
doesn’t exist in Flow. Do you mean
flatMapConcat
or
flatMapMerge
instead? Which one would be the equivalent of
switchMap
in Rx?
s

simon.vergauwen

04/21/2019, 6:28 PM
Reading the comment on
flatMapConcat
and seeing how it’s an alias to
flatMapMerge(concurrency = 1, bufferSize = 1)
I imagine this behaves as
switchMap
but the docs are not extensive enough to confirm that without trying it out unfortunately.
Transforms elements emitted by the original flow by applying transform, that returns another flow, and then concatenating and flattening these flows. This method is identical to flatMapMerge(concurrency = 1, bufferSize = 1)
source: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/
t

Thomas

04/21/2019, 6:30 PM
I found the following issue which shows that
switchMap
still needs to be added: https://github.com/Kotlin/kotlinx.coroutines/issues/1107 So I think
flatMapConcat
works a bit differently but I really don’t know without trying.
s

streetsofboston

04/21/2019, 11:44 PM
First, there'd need to be a way to stop `collect`ing. Is there 'break' like functionality for cancel collecting a Flow?
t

Thomas

04/22/2019, 9:03 AM
@streetsofboston Would throwing a
CancellationException
work?
s

streetsofboston

04/22/2019, 12:03 PM
That may work, but it looks 'hacky' to me... Usually, your code calls
collect
inside a `launch`ed coroutine. Then cancelling the
Job
returned by that
launch
looks more idiomatic to me. Still, I think we'd want a more simple way, more imperative/sequential way of breaking out of a
collect
loop. You can't break out of an Rx
subscribe
either, but since coroutines allow for a more imperitive/sequential way of programming, it would be great if Coroutines'
collect
would allow a 'break' or 'exit' out of the loop. Think about having two plain `Collection<T>`s and writing a
switchMap
for these two collections just using
for
loops...
t

Thomas

04/22/2019, 7:13 PM
@streetsofboston thanks for your detailed reply, I appreciate your explanation. qwwdfsad just made a pull request with an implementation for the switchMap operator, so I no longer need to implement it myself anymore: https://github.com/Kotlin/kotlinx.coroutines/pull/1132 Looks like they are cancelling the
Job
returned by
launch
, just like you explained earlier.
70 Views