runningReduce

Common
JVM
JS
Native
1.4
inline fun <S, T : S> Array<out T>.runningReduce(
    operation: (acc: S, T) -> S
): List<S>

Returns a list containing successive accumulation values generated by applying operation from left to right to each element 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 current accumulator value and the element, and calculates the next accumulator value.

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

Returns a list containing successive accumulation values generated by applying operation from left to right to each element 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 current accumulator value and an element, and calculates the next accumulator value.

Common
JVM
JS
Native
1.4
inline fun <S, T : S> Iterable<T>.runningReduce(
    operation: (acc: S, T) -> S
): List<S>

Returns a list containing successive accumulation values generated by applying operation from left to right to each element 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 current accumulator value and the element, and calculates the next accumulator value.

Common
JVM
JS
Native
1.4
@ExperimentalUnsignedTypes inline fun UIntArray.runningReduce(
    operation: (acc: UInt, UInt) -> UInt
): List<UInt>
@ExperimentalUnsignedTypes inline fun ULongArray.runningReduce(
    operation: (acc: ULong, ULong) -> ULong
): List<ULong>
@ExperimentalUnsignedTypes inline fun UByteArray.runningReduce(
    operation: (acc: UByte, UByte) -> UByte
): List<UByte>
@ExperimentalUnsignedTypes inline fun UShortArray.runningReduce(
    operation: (acc: UShort, UShort) -> UShort
): List<UShort>

Returns a list containing successive accumulation values generated by applying operation from left to right to each element 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 current accumulator value and an element, and calculates the next accumulator value.