scanReduceIndexed
@ExperimentalStdlibApi inline fun <S, T : S> Array<out T>.scanReduceIndexed(
operation: (index: Int, acc: S, T) -> S
): List<S>
Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original array and current accumulator value that starts with the first element of this array.
Note that acc
value passed to operation function should not be mutated;
otherwise it would affect the previous value in resulting list.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scanReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.scanReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().scanReduce { _, _ -> "X" }) // []
//sampleEnd
}
Parameters
operation
- function that takes the index of an element, current accumulator value
and the element itself, and calculates the next accumulator value.
@ExperimentalStdlibApi inline fun ShortArray.scanReduceIndexed(
operation: (index: Int, acc: Short, Short) -> Short
): List<Short>
@ExperimentalStdlibApi inline fun FloatArray.scanReduceIndexed(
operation: (index: Int, acc: Float, Float) -> Float
): List<Float>
@ExperimentalStdlibApi inline fun DoubleArray.scanReduceIndexed(
operation: (index: Int, acc: Double, Double) -> Double
): List<Double>
@ExperimentalStdlibApi inline fun BooleanArray.scanReduceIndexed(
operation: (index: Int, acc: Boolean, Boolean) -> Boolean
): List<Boolean>
Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original array and current accumulator value that starts with the first element of this array.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scanReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.scanReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().scanReduce { _, _ -> "X" }) // []
//sampleEnd
}
Parameters
operation
- function that takes the index of an element, current accumulator value
and the element itself, and calculates the next accumulator value.
Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original collection and current accumulator value that starts with the first element of this collection.
Note that acc
value passed to operation function should not be mutated;
otherwise it would affect the previous value in resulting list.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scanReduce { acc, string -> acc + string }) // [a, ab, abc, abcd]
println(strings.scanReduceIndexed { index, acc, string -> acc + string + index }) // [a, ab1, ab1c2, ab1c2d3]
println(emptyList<String>().scanReduce { _, _ -> "X" }) // []
//sampleEnd
}
Parameters
operation
- function that takes the index of an element, current accumulator value
and the element itself, and calculates the next accumulator value.