https://kotlinlang.org logo
#flow
Title
# flow
m

Michael Marshall

03/03/2022, 4:12 AM
Is there a function that splits a
Flow<UnmappedValue>
into a
Map<SomeKey, Flow<MappedValue>>
? Like the opposite of
merge
? Maybe
explode
or
subFlows
or something like that?
w

wasyl

03/03/2022, 7:19 AM
Not sure how that would work - you’d want to have a
Flow
continuously add to a mutable map?
m

Michael Marshall

03/03/2022, 12:04 PM
I figured it out and made a function, basically I needed this
Copy code
fun <Key, Input, Result> Flow<Input>.toMapOfFlowsBy(mapping: Map<Key, (Key, Input) -> Result>): Map<Key, Flow<Result>> =
    mapping.mapValues { (key, transform) -> map { transform(key, it) } }
You could also do the same thing with a
List
if you wanted
👍 1