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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。