runningFoldIndexed

Common
JVM
JS
Native
1.4
inline fun <T, R> Array<out T>.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, T) -> R
): List<R>

(source)
inline fun <R> ByteArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Byte) -> R
): List<R>

(source)
inline fun <R> ShortArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Short) -> R
): List<R>

(source)
inline fun <R> IntArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Int) -> R
): List<R>

(source)
inline fun <R> LongArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Long) -> R
): List<R>

(source)
inline fun <R> FloatArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Float) -> R
): List<R>

(source)
inline fun <R> DoubleArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Double) -> R
): List<R>

(source)
inline fun <R> BooleanArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Boolean) -> R
): List<R>

(source)
inline fun <R> CharArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, Char) -> R
): List<R>

(source)
@ExperimentalUnsignedTypes inline fun <R> UIntArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, UInt) -> R
): List<R>

(source)
@ExperimentalUnsignedTypes inline fun <R> ULongArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, ULong) -> R
): List<R>

(source)
@ExperimentalUnsignedTypes inline fun <R> UByteArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, UByte) -> R
): List<R>

(source)
@ExperimentalUnsignedTypes inline fun <R> UShortArray.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, UShort) -> R
): List<R>

(source)

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 initial value.

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.runningFold("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.runningFoldIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]

println(emptyList<String>().runningFold("s") { _, _ -> "X" }) // [s]
//sampleEnd
}

Parameters

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

Common
JVM
JS
Native
1.4
inline fun <T, R> Iterable<T>.runningFoldIndexed(
    initial: R,
    operation: (index: Int, acc: R, T) -> R
): List<R>

(source)

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 initial value.

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.runningFold("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.runningFoldIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]

println(emptyList<String>().runningFold("s") { _, _ -> "X" }) // [s]
//sampleEnd
}

Parameters

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