Introduce runningFold and runningReduce operations

runningFold is a synonym for scan,
runningReduce replaces scanReduce.

#KT-38060
This commit is contained in:
Ilya Gorbunov
2020-04-14 14:34:02 +03:00
parent 37277d71de
commit 893021f22b
13 changed files with 1551 additions and 516 deletions
@@ -1357,11 +1357,10 @@ public inline fun CharSequence.reduceRightOrNull(operation: (Char, acc: Char) ->
*
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <R> CharSequence.scan(initial: R, operation: (acc: R, Char) -> R): List<R> {
@SinceKotlin("1.4")
public inline fun <R> CharSequence.runningFold(initial: R, operation: (acc: R, Char) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(length + 1).apply { add(initial) }
var accumulator = initial
@@ -1382,11 +1381,10 @@ public inline fun <R> CharSequence.scan(initial: R, operation: (acc: R, Char) ->
* @param [operation] function that takes the index of a character, current accumulator value
* and the character itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <R> CharSequence.scanIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): List<R> {
@SinceKotlin("1.4")
public inline fun <R> CharSequence.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(length + 1).apply { add(initial) }
var accumulator = initial
@@ -1406,11 +1404,10 @@ public inline fun <R> CharSequence.scanIndexed(initial: R, operation: (index: In
*
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduce(operation: (acc: Char, Char) -> Char): List<Char> {
@SinceKotlin("1.4")
public inline fun CharSequence.runningReduce(operation: (acc: Char, Char) -> Char): List<Char> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<Char>(length).apply { add(accumulator) }
@@ -1431,11 +1428,10 @@ public inline fun CharSequence.scanReduce(operation: (acc: Char, Char) -> Char):
* @param [operation] function that takes the index of a character, current accumulator value
* and the character itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): List<Char> {
@SinceKotlin("1.4")
public inline fun CharSequence.runningReduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): List<Char> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<Char>(length).apply { add(accumulator) }
@@ -1446,6 +1442,55 @@ public inline fun CharSequence.scanReduceIndexed(operation: (index: Int, acc: Ch
return result
}
/**
* 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 [initial] value.
*
* Note that `acc` value passed to [operation] function should not be mutated;
* otherwise it would affect the previous value in resulting list.
*
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <R> CharSequence.scan(initial: R, operation: (acc: R, Char) -> R): List<R> {
return runningFold(initial, operation)
}
/**
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
* to each character, its index in the original char sequence 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.
*
* @param [operation] function that takes the index of a character, current accumulator value
* and the character itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <R> CharSequence.scanIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): List<R> {
return runningFoldIndexed(initial, operation)
}
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduce(operation: (acc: Char, Char) -> Char): List<Char> {
return runningReduce(operation)
}
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): List<Char> {
return runningReduceIndexed(operation)
}
/**
* Returns the sum of all values produced by [selector] function applied to each character in the char sequence.
*/