what is the proper way of transforming a flow<L...
# coroutines
m
what is the proper way of transforming a flow<List<T>> to a flow<T> ?
s
Flatmap?
Copy code
val listflow: Flow<List<Int>> = ...
val itemFlow: Flow<Int> = listFlow.flatMapConcat { it.asFlow() }
(not sure if Collection<T>.asFlow() or something similar exists, but it would not be hard to write yourself if it doesn’t)
m
i mean an operator on flow
managed to make this one:
Copy code
fun <T> Flow<List<T>>.flatten(): Flow<T> {
    return flow {
        collect { values -> values.forEach { value -> emit(value) } }
    }
}
👍 2
i think it should work as intended
a
I’ve used
flatMapConcat { it.asFlow() }
or
flatMapMerge { it.asFlow() }
— flattening flows is potentially a bit more involved than a plain sequence
d
Use
transform
for transforming flows.
Copy code
fun <T> Flow<List<T>>.flatten(): Flow<T> {
    return transform { values ->
        values.forEach { value -> emit(value) }
    }
}
m
ok. thanks
a
transform
will always apply the transforms in series, not in parallel