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,將每個組的鍵與累積組元素的結果相關聯。