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
File diff suppressed because it is too large Load Diff
@@ -2046,11 +2046,10 @@ public inline fun <S, T : S> List<T>.reduceRightOrNull(operation: (T, acc: S) ->
*
* @param [operation] function that takes current accumulator value and an element, 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 <T, R> Iterable<T>.scan(initial: R, operation: (acc: R, T) -> R): List<R> {
@SinceKotlin("1.4")
public inline fun <T, R> Iterable<T>.runningFold(initial: R, operation: (acc: R, T) -> R): List<R> {
val estimatedSize = collectionSizeOrDefault(9)
if (estimatedSize == 0) return listOf(initial)
val result = ArrayList<R>(estimatedSize + 1).apply { add(initial) }
@@ -2072,11 +2071,10 @@ public inline fun <T, R> Iterable<T>.scan(initial: R, operation: (acc: R, T) ->
* @param [operation] function that takes the index of an element, current accumulator value
* and the element 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 <T, R> Iterable<T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): List<R> {
@SinceKotlin("1.4")
public inline fun <T, R> Iterable<T>.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): List<R> {
val estimatedSize = collectionSizeOrDefault(9)
if (estimatedSize == 0) return listOf(initial)
val result = ArrayList<R>(estimatedSize + 1).apply { add(initial) }
@@ -2098,11 +2096,11 @@ public inline fun <T, R> Iterable<T>.scanIndexed(initial: R, operation: (index:
*
* @param [operation] function that takes current accumulator value and the element, 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 <S, T : S> Iterable<T>.scanReduce(operation: (acc: S, T) -> S): List<S> {
public inline fun <S, T : S> Iterable<T>.runningReduce(operation: (acc: S, T) -> S): List<S> {
val iterator = this.iterator()
if (!iterator.hasNext()) return emptyList()
var accumulator: S = iterator.next()
@@ -2124,11 +2122,10 @@ public inline fun <S, T : S> Iterable<T>.scanReduce(operation: (acc: S, T) -> S)
* @param [operation] function that takes the index of an element, current accumulator value
* and the element 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 <S, T : S> Iterable<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S> {
@SinceKotlin("1.4")
public inline fun <S, T : S> Iterable<T>.runningReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S> {
val iterator = this.iterator()
if (!iterator.hasNext()) return emptyList()
var accumulator: S = iterator.next()
@@ -2141,6 +2138,55 @@ public inline fun <S, T : S> Iterable<T>.scanReduceIndexed(operation: (index: In
return result
}
/**
* 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.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <T, R> Iterable<T>.scan(initial: R, operation: (acc: R, T) -> R): List<R> {
return runningFold(initial, operation)
}
/**
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
* to each element, its index in the original collection 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <T, R> Iterable<T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): List<R> {
return runningFoldIndexed(initial, operation)
}
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <S, T : S> Iterable<T>.scanReduce(operation: (acc: S, T) -> S): List<S> {
return runningReduce(operation)
}
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public inline fun <S, T : S> Iterable<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S> {
return runningReduceIndexed(operation)
}
/**
* Returns the sum of all values produced by [selector] function applied to each element in the collection.
*/
@@ -1464,15 +1464,14 @@ public inline fun <S, T : S> Sequence<T>.reduceOrNull(operation: (acc: S, T) ->
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scan
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <T, R> Sequence<T>.scan(initial: R, operation: (acc: R, T) -> R): Sequence<R> {
@SinceKotlin("1.4")
public fun <T, R> Sequence<T>.runningFold(initial: R, operation: (acc: R, T) -> R): Sequence<R> {
return sequence {
yield(initial)
var accumulator = initial
for (element in this@scan) {
for (element in this@runningFold) {
accumulator = operation(accumulator, element)
yield(accumulator)
}
@@ -1493,16 +1492,15 @@ public fun <T, R> Sequence<T>.scan(initial: R, operation: (acc: R, T) -> R): Seq
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scan
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <T, R> Sequence<T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): Sequence<R> {
@SinceKotlin("1.4")
public fun <T, R> Sequence<T>.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): Sequence<R> {
return sequence {
yield(initial)
var index = 0
var accumulator = initial
for (element in this@scanIndexed) {
for (element in this@runningFoldIndexed) {
accumulator = operation(checkIndexOverflow(index++), accumulator, element)
yield(accumulator)
}
@@ -1520,11 +1518,11 @@ public fun <T, R> Sequence<T>.scanIndexed(initial: R, operation: (index: Int, ac
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <S, T : S> Sequence<T>.scanReduce(operation: (acc: S, T) -> S): Sequence<S> {
public fun <S, T : S> Sequence<T>.runningReduce(operation: (acc: S, T) -> S): Sequence<S> {
return sequence {
val iterator = iterator()
if (iterator.hasNext()) {
@@ -1550,11 +1548,10 @@ public fun <S, T : S> Sequence<T>.scanReduce(operation: (acc: S, T) -> S): Seque
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <S, T : S> Sequence<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): Sequence<S> {
@SinceKotlin("1.4")
public fun <S, T : S> Sequence<T>.runningReduceIndexed(operation: (index: Int, acc: S, T) -> S): Sequence<S> {
return sequence {
val iterator = iterator()
if (iterator.hasNext()) {
@@ -1569,6 +1566,63 @@ public fun <S, T : S> Sequence<T>.scanReduceIndexed(operation: (index: Int, acc:
}
}
/**
* Returns a sequence 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 sequence.
* The [initial] value should also be immutable (or should not be mutated)
* as it may be passed to [operation] function later because of sequence's lazy nature.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <T, R> Sequence<T>.scan(initial: R, operation: (acc: R, T) -> R): Sequence<R> {
return runningFold(initial, operation)
}
/**
* Returns a sequence containing successive accumulation values generated by applying [operation] from left to right
* to each element, its index in the original 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 sequence.
* The [initial] value should also be immutable (or should not be mutated)
* as it may be passed to [operation] function later because of sequence's lazy nature.
*
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <T, R> Sequence<T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): Sequence<R> {
return runningFoldIndexed(initial, operation)
}
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <S, T : S> Sequence<T>.scanReduce(operation: (acc: S, T) -> S): Sequence<S> {
return runningReduce(operation)
}
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public fun <S, T : S> Sequence<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): Sequence<S> {
return runningReduceIndexed(operation)
}
/**
* Returns the sum of all values produced by [selector] function applied to each element in the sequence.
*
@@ -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.
*/
+428 -216
View File
@@ -6719,13 +6719,12 @@ public inline fun UShortArray.reduceRightOrNull(operation: (UShort, acc: UShort)
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UIntArray.scan(initial: R, operation: (acc: R, UInt) -> R): List<R> {
public inline fun <R> UIntArray.runningFold(initial: R, operation: (acc: R, UInt) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
@@ -6736,6 +6735,408 @@ public inline fun <R> UIntArray.scan(initial: R, operation: (acc: R, UInt) -> R)
return result
}
/**
* 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.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> ULongArray.runningFold(initial: R, operation: (acc: R, ULong) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 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.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UByteArray.runningFold(initial: R, operation: (acc: R, UByte) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 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.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UShortArray.runningFold(initial: R, operation: (acc: R, UShort) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 element, its index in the original array 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UIntArray.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, UInt) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 element, its index in the original array 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> ULongArray.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, ULong) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 element, its index in the original array 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UByteArray.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, UByte) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 element, its index in the original array 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningFold
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UShortArray.runningFoldIndexed(initial: R, operation: (index: Int, acc: R, UShort) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 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 element and current accumulator value that starts with the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.runningReduce(operation: (acc: UInt, UInt) -> UInt): List<UInt> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UInt>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element and current accumulator value that starts with the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.runningReduce(operation: (acc: ULong, ULong) -> ULong): List<ULong> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<ULong>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element and current accumulator value that starts with the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.runningReduce(operation: (acc: UByte, UByte) -> UByte): List<UByte> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UByte>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element and current accumulator value that starts with the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.runningReduce(operation: (acc: UShort, UShort) -> UShort): List<UShort> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UShort>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element, its index in the original array and current accumulator value that starts with the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.runningReduceIndexed(operation: (index: Int, acc: UInt, UInt) -> UInt): List<UInt> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UInt>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element, its index in the original array and current accumulator value that starts with the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.runningReduceIndexed(operation: (index: Int, acc: ULong, ULong) -> ULong): List<ULong> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<ULong>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element, its index in the original array and current accumulator value that starts with the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.runningReduceIndexed(operation: (index: Int, acc: UByte, UByte) -> UByte): List<UByte> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UByte>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 element, its index in the original array and current accumulator value that starts with the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.runningReduceIndexed(operation: (index: Int, acc: UShort, UShort) -> UShort): List<UShort> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UShort>(size).apply { add(accumulator) }
for (index in 1 until size) {
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 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.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scan
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UIntArray.scan(initial: R, operation: (acc: R, UInt) -> R): List<R> {
return runningFold(initial, operation)
}
/**
* 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.
@@ -6752,14 +7153,7 @@ public inline fun <R> UIntArray.scan(initial: R, operation: (acc: R, UInt) -> R)
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> ULongArray.scan(initial: R, operation: (acc: R, ULong) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (element in this) {
accumulator = operation(accumulator, element)
result.add(accumulator)
}
return result
return runningFold(initial, operation)
}
/**
@@ -6778,14 +7172,7 @@ public inline fun <R> ULongArray.scan(initial: R, operation: (acc: R, ULong) ->
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UByteArray.scan(initial: R, operation: (acc: R, UByte) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (element in this) {
accumulator = operation(accumulator, element)
result.add(accumulator)
}
return result
return runningFold(initial, operation)
}
/**
@@ -6804,14 +7191,7 @@ public inline fun <R> UByteArray.scan(initial: R, operation: (acc: R, UByte) ->
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UShortArray.scan(initial: R, operation: (acc: R, UShort) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (element in this) {
accumulator = operation(accumulator, element)
result.add(accumulator)
}
return result
return runningFold(initial, operation)
}
/**
@@ -6831,14 +7211,7 @@ public inline fun <R> UShortArray.scan(initial: R, operation: (acc: R, UShort) -
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UIntArray.scanIndexed(initial: R, operation: (index: Int, acc: R, UInt) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (index in indices) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningFoldIndexed(initial, operation)
}
/**
@@ -6858,14 +7231,7 @@ public inline fun <R> UIntArray.scanIndexed(initial: R, operation: (index: Int,
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> ULongArray.scanIndexed(initial: R, operation: (index: Int, acc: R, ULong) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (index in indices) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningFoldIndexed(initial, operation)
}
/**
@@ -6885,14 +7251,7 @@ public inline fun <R> ULongArray.scanIndexed(initial: R, operation: (index: Int,
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UByteArray.scanIndexed(initial: R, operation: (index: Int, acc: R, UByte) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (index in indices) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningFoldIndexed(initial, operation)
}
/**
@@ -6912,226 +7271,79 @@ public inline fun <R> UByteArray.scanIndexed(initial: R, operation: (index: Int,
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R> UShortArray.scanIndexed(initial: R, operation: (index: Int, acc: R, UShort) -> R): List<R> {
if (isEmpty()) return listOf(initial)
val result = ArrayList<R>(size + 1).apply { add(initial) }
var accumulator = initial
for (index in indices) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningFoldIndexed(initial, operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.scanReduce(operation: (acc: UInt, UInt) -> UInt): List<UInt> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UInt>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduce(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.scanReduce(operation: (acc: ULong, ULong) -> ULong): List<ULong> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<ULong>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduce(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.scanReduce(operation: (acc: UByte, UByte) -> UByte): List<UByte> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UByte>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduce(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduce instead.", ReplaceWith("runningReduce(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.scanReduce(operation: (acc: UShort, UShort) -> UShort): List<UShort> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UShort>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduce(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.scanReduceIndexed(operation: (index: Int, acc: UInt, UInt) -> UInt): List<UInt> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UInt>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduceIndexed(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.scanReduceIndexed(operation: (index: Int, acc: ULong, ULong) -> ULong): List<ULong> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<ULong>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduceIndexed(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.scanReduceIndexed(operation: (index: Int, acc: UByte, UByte) -> UByte): List<UByte> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UByte>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduceIndexed(operation)
}
/**
* 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 the first element of this array.
*
* 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 an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.scanReduce
*/
@Deprecated("Use runningReduceIndexed instead.", ReplaceWith("runningReduceIndexed(operation)"))
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.scanReduceIndexed(operation: (index: Int, acc: UShort, UShort) -> UShort): List<UShort> {
if (isEmpty()) return emptyList()
var accumulator = this[0]
val result = ArrayList<UShort>(size).apply { add(accumulator) }
for (index in 1 until size) {
accumulator = operation(index, accumulator, this[index])
result.add(accumulator)
}
return result
return runningReduceIndexed(operation)
}
/**