reduce
所在位置是kotlin.collections.reduce
,其相關用法介紹如下。
用法一
@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 從左到右應用於當前累加器值和每個元素。
如果此集合為空,則引發異常。如果集合可以按預期方式為空,請改用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
參數
用法三
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,將每個組的鍵與累積組元素的結果相關聯。
相關用法
- Kotlin reduceRight用法及代碼示例
- Kotlin reduceIndexed用法及代碼示例
- Kotlin reduceTo用法及代碼示例
- Kotlin reduceRightIndexed用法及代碼示例
- Kotlin reduceIndexedOrNull用法及代碼示例
- Kotlin reduce用法及代碼示例
- Kotlin reduceRightIndexedOrNull用法及代碼示例
- Kotlin reduceRightOrNull用法及代碼示例
- Kotlin reduceOrNull用法及代碼示例
- Kotlin reversed用法及代碼示例
- Kotlin replaceFirstChar用法及代碼示例
- Kotlin replace用法及代碼示例
- Kotlin runningReduceIndexed用法及代碼示例
- Kotlin rangeTo用法及代碼示例
- Kotlin runningFoldIndexed用法及代碼示例
- Kotlin runningReduce用法及代碼示例
- Kotlin runningFold用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.reduce。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。