W3cubDocs

/Kotlin

fold

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, R> Array<out T>.fold(
    initial: R, 
    operation: (acc: R, T) -> R
): R
inline fun <R> ByteArray.fold(
    initial: R, 
    operation: (acc: R, Byte) -> R
): R
inline fun <R> ShortArray.fold(
    initial: R, 
    operation: (acc: R, Short) -> R
): R
inline fun <R> IntArray.fold(
    initial: R, 
    operation: (acc: R, Int) -> R
): R
inline fun <R> LongArray.fold(
    initial: R, 
    operation: (acc: R, Long) -> R
): R
inline fun <R> FloatArray.fold(
    initial: R, 
    operation: (acc: R, Float) -> R
): R
inline fun <R> DoubleArray.fold(
    initial: R, 
    operation: (acc: R, Double) -> R
): R
inline fun <R> BooleanArray.fold(
    initial: R, 
    operation: (acc: R, Boolean) -> R
): R
inline fun <R> CharArray.fold(
    initial: R, 
    operation: (acc: R, Char) -> R
): R
inline fun <T, R> Iterable<T>.fold(
    initial: R, 
    operation: (acc: R, T) -> R
): R
@ExperimentalUnsignedTypes inline fun <R> UIntArray.fold(
    initial: R, 
    operation: (acc: R, UInt) -> R
): R
@ExperimentalUnsignedTypes inline fun <R> ULongArray.fold(
    initial: R, 
    operation: (acc: R, ULong) -> R
): R
@ExperimentalUnsignedTypes inline fun <R> UByteArray.fold(
    initial: R, 
    operation: (acc: R, UByte) -> R
): R
@ExperimentalUnsignedTypes inline fun <R> UShortArray.fold(
    initial: R, 
    operation: (acc: R, UShort) -> R
): R

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element.

Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)
inline fun <T, K, R> Grouping<T, K>.fold(
    initialValueSelector: (key: K, element: T) -> R, 
    operation: (key: K, accumulator: R, element: T) -> R
): Map<K, R>

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 a new 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() }
    .fold({ key, _ -> key to mutableListOf<String>() },
          { _, accumulator, element ->
              accumulator.also { (_, list) -> if (element.length % 2 == 0) list.add(element) }
          })

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

Parameters

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

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

Return a 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> Grouping<T, K>.fold(
    initialValue: R, 
    operation: (accumulator: R, element: T) -> R
): Map<K, R>

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 a new map. An initial value of accumulator is the same initialValue for each 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() }
    .fold(listOf<String>()) { acc, e -> if (e.length % 2 == 0) acc + e else acc }

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

Parameters

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

Return a Map associating the key of each group with the result of accumulating the group elements.

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html