foldTo

Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)
inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
    destination: M, 
    initialValueSelector: (key: K, element: T) -> R, 
    operation: (key: K, accumulator: R, element: T) -> R
): M

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map. An initial value of accumulator is provided by initialValueSelector function.



fun main(args: Array<String>) {
//sampleStart
val fruits = listOf("cherry", "blueberry", "citrus", "apple", "apricot", "banana", "coconut")

val evenFruits = fruits.groupingBy { it.first() }
    .foldTo(mutableMapOf(), { key, _: String -> key to mutableListOf<String>() },
            { _, accumulator, element ->
                if (element.length % 2 == 0) accumulator.second.add(element)
                accumulator
            })

val sorted = evenFruits.values.sortedBy { it.first }
println(sorted) // [(a, []), (b, [banana]), (c, [cherry, citrus])]

evenFruits.clear() // evenFruits is a mutable map
//sampleEnd
}

Parameters

initialValueSelector -

a function that provides an initial value of accumulator for each group. It's invoked with parameters:

  • key: the key of the group;
  • element: the first element being encountered in that group.

If the destination map already has a value corresponding to some key, that value is used as an initial value of the accumulator for that group and the initialValueSelector function is not called for that group.

operation - a function that is invoked on each element with the following parameters:

Return the destination map associating the key of each group with the result of accumulating the group elements.

Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)
inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
    destination: M, 
    initialValue: R, 
    operation: (accumulator: R, element: T) -> R
): M

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map. An initial value of accumulator is the same initialValue for each group.

If the destination map already has a value corresponding to the key of some group, that value is used as an initial value of the accumulator for that group.



fun main(args: Array<String>) {
//sampleStart
val fruits = listOf("apple", "apricot", "banana", "blueberry", "cherry", "coconut")

// collect only even length Strings
val evenFruits = fruits.groupingBy { it.first() }
    .foldTo(mutableMapOf(), emptyList<String>()) { acc, e -> if (e.length % 2 == 0) acc + e else acc }

println(evenFruits) // {a=[], b=[banana], c=[cherry]}

evenFruits.clear() // evenFruits is a mutable map
//sampleEnd
}

Parameters

operation - a function that is invoked on each element with the following parameters:

Return the destination map associating the key of each group with the result of accumulating the group elements.

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-to.html