org.nield.kotlinstatistics.Aggregation.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-statistics Show documentation
Show all versions of kotlin-statistics Show documentation
Statistical and analytical extensions for Kotlin
package org.nield.kotlinstatistics
inline fun Sequence.groupApply(crossinline keySelector: (T) -> K, crossinline aggregation: (Iterable) -> R): Map {
val map = mutableMapOf>()
for (item in this) {
val key = keySelector(item)
val list = map.computeIfAbsent(key) { mutableListOf() }
list += item
}
val aggregatedMap = mutableMapOf()
for ((key, value) in map) {
aggregatedMap.put(key, aggregation(value))
}
return aggregatedMap
}
inline fun Sequence.groupApply(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> V, crossinline aggregation: (Iterable) -> R): Map {
val map = mutableMapOf>()
for (item in this) {
val key = keySelector(item)
val list = map.computeIfAbsent(key) { mutableListOf() }
list += valueSelector(item)
}
val aggregatedMap = mutableMapOf()
for (entry in map.entries) {
aggregatedMap.put(entry.key, aggregation(entry.value))
}
return aggregatedMap
}