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

用法:

inline fun <S, T : S> Sequence<T>.reduceIndexed(
    operation: (index: Int, acc: S, T) -> S
): S

从第一个元素开始累加值,并将 operation 从左到右应用于当前累加器值以及原始序列中的每个元素及其索引。

如果此序列为空,则引发异常。如果序列可以按预期方式为空,请改用reduceIndexedOrNull。当接收器为空时,它返回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-

获取元素索引、当前累加器值和元素本身的函数,并计算下一个累加器值。

操作是 terminal