All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nield.kotlinstatistics.Aggregation.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy