fun <S, T : S> Sequence<T>.runningReduceIndexed( operation: (index: Int, acc: S, T) -> S ): Sequence<S>
Returns a sequence containing successive accumulation values generated by applying operation from left to right to each element, its index in the original sequence and current accumulator value that starts with the first element of this sequence.
Note that acc
value passed to operation function should not be mutated; otherwise it would affect the previous value in resulting sequence.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.runningReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.runningReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().runningReduce { _, _ -> "X" }) // []
//sampleEnd
}
operation
-
function that takes the index of an element, current accumulator value and the element itself, and calculates the next accumulator value.
The operation is intermediate and stateless.
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/running-reduce-indexed.html