mapReduce

inline fun <K, V, T : R, R> Map<out K, V>.mapReduce(transform: (Map.Entry<K, V>) -> T, operation: (acc: R, T) -> R): R

Lazily applies the given transform function to each entry of the map getting a new element and accumulates value starting with the first element and applying operation one-by-one to current accumulator value and each element.

Iterator of the map's entries is obtained with entries.iterator() method. Thus, iteration order is specified by the map's entries set iterator implementation.

Throws an exception if this map is empty. If the map can be empty in an expected way, please use mapReduceOrNull instead. It returns null when its receiver is empty.

Parameters

transform

function which transforms each key-value pair to a new element which will be processed with given operation.

operation

function that takes current accumulator value and transformed into a new element entry of the map and calculates the next accumulator value.