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

com.grab.grazel.util.Map.kt Maven / Gradle / Ivy

The newest version!
package com.grab.grazel.util

/***
 * Merge of collection of `Maps` into a single map by using `merger` function to merge values
 * for keys that are same.
 */
fun  Iterable>.merge(merger: (prev: V, next: V) -> V): Map {
    return fold(mutableMapOf()) { acc, map ->
        map.forEach { (key, value) ->
            if (acc.containsKey(key)) {
                acc[key] = merger(acc.getValue(key), value)
            } else {
                acc[key] = value
            }
        }
        acc
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy