I have a flow like this: Flow<List<Transaction>>
Each Transaction object has a Category object
What i want is an extension function that groups the transactions per Category
This is what i've tried:
inline fun <T, K> Flow<Iterable<T>>.groupIterableBy(crossinline keySelector: (T) -> K): Flow<Map<K, MutableList<T>>> = map {
val storage = HashMap<K, MutableList<T>>()
it.map{ element ->
val key = keySelector(element)
if (storage[key] == null){
storage[key] = mutableListOf()
}
storage[key]!!.add(element)
}
return@map storage
}
This works great, but i don't feel like this is programmed in a clean way. Does anyone have suggestions for making this function cleaner?