scan

inline fun <T, R> Array<out T>.scan(initial: R, operation: (acc: R, T) -> R): List<R>(source)
inline fun <R> ByteArray.scan(initial: R, operation: (acc: R, Byte) -> R): List<R>(source)
inline fun <R> ShortArray.scan(initial: R, operation: (acc: R, Short) -> R): List<R>(source)
inline fun <R> IntArray.scan(initial: R, operation: (acc: R, Int) -> R): List<R>(source)
inline fun <R> LongArray.scan(initial: R, operation: (acc: R, Long) -> R): List<R>(source)
inline fun <R> FloatArray.scan(initial: R, operation: (acc: R, Float) -> R): List<R>(source)
inline fun <R> DoubleArray.scan(initial: R, operation: (acc: R, Double) -> R): List<R>(source)
inline fun <R> BooleanArray.scan(initial: R, operation: (acc: R, Boolean) -> R): List<R>(source)
inline fun <R> CharArray.scan(initial: R, operation: (acc: R, Char) -> R): List<R>(source)
inline fun <T, R> Iterable<T>.scan(initial: R, operation: (acc: R, T) -> R): List<R>(source)
inline fun <R> UIntArray.scan(initial: R, operation: (acc: R, UInt) -> R): List<R>(source)
inline fun <R> ULongArray.scan(initial: R, operation: (acc: R, ULong) -> R): List<R>(source)
inline fun <R> UByteArray.scan(initial: R, operation: (acc: R, UByte) -> R): List<R>(source)
inline fun <R> UShortArray.scan(initial: R, operation: (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 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.

Since Kotlin

1.4

Parameters

operation

function that takes current accumulator value and an element, and calculates the next accumulator value.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val strings = listOf("a", "b", "c", "d")
println(strings.scan("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.scanIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]

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