running Reduce
inline fun CharSequence.runningReduce(operation: (acc: Char, Char) -> Char): List<Char>
Content copied to clipboard
Returns a list containing successive accumulation values generated by applying operation from left to right to each character and current accumulator value that starts with the first character of this char sequence.
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
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.runningReduce { acc, string -> acc + string }, "[a, ab, abc, abcd]")
assertPrints(strings.runningReduceIndexed { index, acc, string -> acc + string + index }, "[a, ab1, ab1c2, ab1c2d3]")
assertPrints(emptyList<String>().runningReduce { _, _ -> "X" }, "[]")
//sampleEnd
}
Parameters
operation
function that takes current accumulator value and a character, and calculates the next accumulator value.