scanReduce

Common
JVM
JS
Native
1.3
@ExperimentalStdlibApi inline fun <S, T : S> Array<out T>.scanReduce(
    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.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 current accumulator value and the element, and calculates the next accumulator value.

Common
JVM
JS
Native
1.3
@ExperimentalStdlibApi inline fun ByteArray.scanReduce(
    operation: (acc: Byte, Byte) -> Byte
): List<Byte>
@ExperimentalStdlibApi inline fun ShortArray.scanReduce(
    operation: (acc: Short, Short) -> Short
): List<Short>
@ExperimentalStdlibApi inline fun IntArray.scanReduce(
    operation: (acc: Int, Int) -> Int
): List<Int>
@ExperimentalStdlibApi inline fun LongArray.scanReduce(
    operation: (acc: Long, Long) -> Long
): List<Long>
@ExperimentalStdlibApi inline fun FloatArray.scanReduce(
    operation: (acc: Float, Float) -> Float
): List<Float>
@ExperimentalStdlibApi inline fun DoubleArray.scanReduce(
    operation: (acc: Double, Double) -> Double
): List<Double>
@ExperimentalStdlibApi inline fun BooleanArray.scanReduce(
    operation: (acc: Boolean, Boolean) -> Boolean
): List<Boolean>
@ExperimentalStdlibApi inline fun CharArray.scanReduce(
    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.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 current accumulator value and an element, and calculates the next accumulator value.

Common
JVM
JS
Native
1.3
@ExperimentalStdlibApi inline fun <S, T : S> Iterable<T>.scanReduce(
    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.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 current accumulator value and the element, and calculates the next accumulator value.

Common
JVM
JS
Native
1.3
@ExperimentalStdlibApi @ExperimentalUnsignedTypes inline fun UIntArray.scanReduce(
    operation: (acc: UInt, UInt) -> UInt
): List<UInt>
@ExperimentalStdlibApi @ExperimentalUnsignedTypes inline fun ULongArray.scanReduce(
    operation: (acc: ULong, ULong) -> ULong
): List<ULong>
@ExperimentalStdlibApi @ExperimentalUnsignedTypes inline fun UByteArray.scanReduce(
    operation: (acc: UByte, UByte) -> UByte
): List<UByte>
@ExperimentalStdlibApi @ExperimentalUnsignedTypes inline fun UShortArray.scanReduce(
    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.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 current accumulator value and an element, and calculates the next accumulator value.