W3cubDocs

/Kotlin

runningReduceIndexed

Platform and version requirements: JVM (1.4), JS (1.4), Native (1.4)
inline fun <S, T : S> Array<out T>.runningReduceIndexed(
    operation: (index: Int, acc: S, T) -> S
): List<S>
@ExperimentalUnsignedTypes inline fun UIntArray.runningReduceIndexed(
    operation: (index: Int, acc: UInt, UInt) -> UInt
): List<UInt>
@ExperimentalUnsignedTypes inline fun ULongArray.runningReduceIndexed(
    operation: (index: Int, acc: ULong, ULong) -> ULong
): List<ULong>
@ExperimentalUnsignedTypes inline fun UByteArray.runningReduceIndexed(
    operation: (index: Int, acc: UByte, UByte) -> UByte
): List<UByte>
@ExperimentalUnsignedTypes inline fun UShortArray.runningReduceIndexed(
    operation: (index: Int, acc: UShort, UShort) -> UShort
): List<UShort>

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.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
}

Parameters

operation - function that takes the index of an element, current accumulator value and the element itself, and calculates the next accumulator value.

Platform and version requirements: JVM (1.4), JS (1.4), Native (1.4)
inline fun ByteArray.runningReduceIndexed(
    operation: (index: Int, acc: Byte, Byte) -> Byte
): List<Byte>
inline fun ShortArray.runningReduceIndexed(
    operation: (index: Int, acc: Short, Short) -> Short
): List<Short>
inline fun IntArray.runningReduceIndexed(
    operation: (index: Int, acc: Int, Int) -> Int
): List<Int>
inline fun LongArray.runningReduceIndexed(
    operation: (index: Int, acc: Long, Long) -> Long
): List<Long>
inline fun FloatArray.runningReduceIndexed(
    operation: (index: Int, acc: Float, Float) -> Float
): List<Float>
inline fun DoubleArray.runningReduceIndexed(
    operation: (index: Int, acc: Double, Double) -> Double
): List<Double>
inline fun BooleanArray.runningReduceIndexed(
    operation: (index: Int, acc: Boolean, Boolean) -> Boolean
): List<Boolean>
inline fun CharArray.runningReduceIndexed(
    operation: (index: Int, acc: Char, Char) -> Char
): List<Char>

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.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
}

Parameters

operation - function that takes the index of an element, current accumulator value and the element itself, and calculates the next accumulator value.

Platform and version requirements: JVM (1.4), JS (1.4), Native (1.4)
inline fun <S, T : S> Iterable<T>.runningReduceIndexed(
    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 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.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
}

Parameters

operation - function that takes the index of an element, current accumulator value and the element itself, and calculates the next accumulator value.

© 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.collections/running-reduce-indexed.html