Is there a cleaner way of collecting two Flows? ``...
# stdlib
m
Is there a cleaner way of collecting two Flows?
Copy code
combine(flowA, flowB) { a, b -> a to b }.collect { (a, b) -> println("$a, $b") }
I'm looking to remove the "useless" transform step that the
combine
function requires.
p
Why not combine to
Unit
and
.collect()
?
Copy code
combine(flowA, flowB) { a, b -> println("$a, $b") }.collect()
(there's probably a reason... genuinely intrigued if there is)
k
Could
combineTransform
be what you're looking for?
e
combineTransform
is basically the same as
combine
with a lambda for this use case, although you can emit nothing which I guess is slightly less work
m
Hmm, I'm not seeing how to apply
combineTransform
here in a way that would make it shorter.
e
Copy code
combineTransform(flowA, flowB) { a, b -> println("$a, $b") }.collect()
same as Phil's suggestion, but it skips the discarded emissions
m
Phil's suggestion works fine, but
combineTransform
makes me manually specify types so it becomes
combineTransform<TypeA, TypeB, Unit>(flowA, flowB) { a, b -> println("$a, $b") }.collect()
Which becomes longer than the original. With
combine
the type parameters are inferred correctly.
e
you should be able to write
Copy code
.combineTransform<_, _, Nothing> { a, b ->
it is longer, but it more clearly marks that the transform doesn't actually produce any output
👍 1
m
It just all feels like a hack to me because it's "misusing" the transform lambda as the terminal operation instead of transforming data.