Scan functions for Sequences and Iterable #KT-7657

This commit is contained in:
Abduqodiri Qurbonzoda
2020-01-24 04:42:19 +03:00
parent 0d7e641736
commit ed7b8e9b85
16 changed files with 2411 additions and 0 deletions
@@ -1288,6 +1288,104 @@ public inline fun CharSequence.reduceRightOrNull(operation: (Char, acc: Char) ->
return accumulator
}
/**
* 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> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(length + 1).apply { add(initial) }
var accumulator = initial
for (element in this) {
accumulator = operation(accumulator, element)
result.add(accumulator)
}
return result
}
/**
* 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> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(length + 1).apply { add(initial) }
var accumulator = initial
for (index in indices) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
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 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.
*
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduce(operation: (acc: Char, Char) -> Char): List<Char> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<Char>(length).apply { add(accumulator) }
for (index in 1 until length) {
accumulator = operation(accumulator, this[index])
result.add(accumulator)
}
return result
}
/**
* 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 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.
*
* @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
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun CharSequence.scanReduceIndexed(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) }
for (index in 1 until length) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
}
/**
* Returns the sum of all values produced by [selector] function applied to each character in the char sequence.
*/