reduce所在位置是kotlin.collections.reduce,其相关用法介绍如下。

用法一

inline fun <S, T : S> Array<out T>.reduce(
    operation: (acc: S, T) -> S
): S
inline fun ByteArray.reduce(
    operation: (acc: Byte, Byte) -> Byte
): Byte
inline fun ShortArray.reduce(
    operation: (acc: Short, Short) -> Short
): Short
inline fun IntArray.reduce(
    operation: (acc: Int, Int) -> Int
): Int
inline fun LongArray.reduce(
    operation: (acc: Long, Long) -> Long
): Long
inline fun FloatArray.reduce(
    operation: (acc: Float, Float) -> Float
): Float
inline fun DoubleArray.reduce(
    operation: (acc: Double, Double) -> Double
): Double
inline fun BooleanArray.reduce(
    operation: (acc: Boolean, Boolean) -> Boolean
): Boolean
inline fun CharArray.reduce(
    operation: (acc: Char, Char) -> Char
): Char
@ExperimentalUnsignedTypes inline fun UIntArray.reduce(
    operation: (acc: UInt, UInt) -> UInt
): UInt
@ExperimentalUnsignedTypes inline fun ULongArray.reduce(
    operation: (acc: ULong, ULong) -> ULong
): ULong
@ExperimentalUnsignedTypes inline fun UByteArray.reduce(
    operation: (acc: UByte, UByte) -> UByte
): UByte
@ExperimentalUnsignedTypes inline fun UShortArray.reduce(
    operation: (acc: UShort, UShort) -> UShort
): UShort

从第一个元素开始累加值,并将 operation 从左到右应用于当前累加器值和每个元素。

如果此数组为空,则引发异常。如果数组可以按预期方式为空,请改用reduceOrNull。当接收器为空时,它返回null

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.reduce { acc, string -> acc + string }) // abcd
println(strings.reduceIndexed { index, acc, string -> acc + string + index }) // ab1c2d3

// emptyList<Int>().reduce { _, _ -> 0 } //  will fail
//sampleEnd
}

输出:

abcd
ab1c2d3

参数

operation- 获取当前累加器值和一个元素并计算下一个累加器值的函数。

用法二

inline fun <S, T : S> Iterable<T>.reduce(
    operation: (acc: S, T) -> S
): S

从第一个元素开始累加值,并将 operation 从左到右应用于当前累加器值和每个元素。

如果此集合为空,则引发异常。如果集合可以按预期方式为空,请改用reduceOrNull。当接收器为空时,它返回null

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.reduce { acc, string -> acc + string }) // abcd
println(strings.reduceIndexed { index, acc, string -> acc + string + index }) // ab1c2d3

// emptyList<Int>().reduce { _, _ -> 0 } //  will fail
//sampleEnd
}

输出:

abcd
ab1c2d3

参数

operation- 获取当前累加器值和一个元素并计算下一个累加器值的函数。

用法三

inline fun <S, T : S, K> Grouping<T, K>.reduce(
    operation: (key: K, accumulator: S, element: T) -> S
): Map<K, S>

按键对Grouping源中的元素进行分组,并从组的第二个元素开始按顺序对每个组的元素应用减少operation,将先前累积的值和当前元素作为参数传递,并将结果存储在一张新Map。累加器的初始值是组的第一个元素。

例子:



fun main(args: Array<String>) {
//sampleStart
val animals = listOf("raccoon", "reindeer", "cow", "camel", "giraffe", "goat")

// grouping by first char and collect only max of contains vowels
val compareByVowelCount = compareBy { s: String -> s.count { it in "aeiou" } }

val maxVowels = animals.groupingBy { it.first() }.reduce { _, a, b -> maxOf(a, b, compareByVowelCount) }

println(maxVowels) // {r=reindeer, c=camel, g=giraffe}
//sampleEnd
}

输出:

{r=reindeer, c=camel, g=giraffe}

参数

operation- 使用以下参数对组的每个后续元素调用的函数:

返回一个Map,将每个组的键与累积组元素的结果相关联。