scanIndexed

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.

inline fun <T, R> Array<out T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): List<R>(source)
val numbers = arrayOf(1, 2, 3, 4)

val cumulative = numbers.scanIndexed(0) { index, acc, value ->
    acc + (index + 1) * value   // accumulate using index and value
}

println(cumulative)   // Output: [0, 1, 5, 14, 30]

Source