Introduce runningFold and runningReduce operations
runningFold is a synonym for scan, runningReduce replaces scanReduce. #KT-38060
This commit is contained in:
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.
|
||||
*/
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -722,12 +722,21 @@ class Collections {
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun scanReduce() {
|
||||
fun runningFold() {
|
||||
val strings = listOf("a", "b", "c", "d")
|
||||
assertPrints(strings.scanReduce { acc, string -> acc + string }, "[a, ab, abc, abcd]")
|
||||
assertPrints(strings.scanReduceIndexed { index, acc, string -> acc + string + index }, "[a, ab1, ab1c2, ab1c2d3]")
|
||||
assertPrints(strings.runningFold("s") { acc, string -> acc + string }, "[s, sa, sab, sabc, sabcd]")
|
||||
assertPrints(strings.runningFoldIndexed("s") { index, acc, string -> acc + string + index }, "[s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]")
|
||||
|
||||
assertPrints(emptyList<String>().scanReduce { _, _ -> "X" }, "[]")
|
||||
assertPrints(emptyList<String>().runningFold("s") { _, _ -> "X" }, "[s]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun runningReduce() {
|
||||
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" }, "[]")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1141,6 +1141,27 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun runningFold() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
|
||||
// Array<T>
|
||||
assertEquals(expected, Array(size) { it }.runningFold("") { acc, e -> acc + e })
|
||||
// Primitive Arrays
|
||||
assertEquals(expected, ByteArray(size) { it.toByte() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, CharArray(size) { '0' + it }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, ShortArray(size) { it.toShort() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, IntArray(size) { it }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, LongArray(size) { it.toLong() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, FloatArray(size) { it.toFloat() }.runningFold("") { acc, e -> acc + e.toInt() })
|
||||
assertEquals(expected, DoubleArray(size) { it.toDouble() }.runningFold("") { acc, e -> acc + e.toInt() })
|
||||
assertEquals(
|
||||
expected.map { it.map { c -> c.toInt() % 2 == 0 }.joinToString(separator = "") },
|
||||
BooleanArray(size) { it % 2 == 0 }.runningFold("") { acc, e -> acc + e }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
@@ -1185,92 +1206,137 @@ class ArraysTest {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun scanReduce() {
|
||||
fun runningFoldIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).take(size)
|
||||
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
|
||||
// Array<T>
|
||||
assertEquals(
|
||||
expected,
|
||||
Array(size) { it }.scanReduce { acc, e -> acc + e }
|
||||
Array(size) { 'a' + it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
|
||||
)
|
||||
// Primitive Arrays
|
||||
assertEquals(
|
||||
expected.map { it.toByte() },
|
||||
ByteArray(size) { it.toByte() }.scanReduce { acc, e -> (acc + e).toByte() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toChar() },
|
||||
CharArray(size) { it.toChar() }.scanReduce { acc, e -> acc + e.toInt() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toShort() },
|
||||
ShortArray(size) { it.toShort() }.scanReduce { acc, e -> (acc + e).toShort() }
|
||||
expected,
|
||||
ByteArray(size) { it.toByte() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected,
|
||||
IntArray(size) { it }.scanReduce { acc, e -> acc + e }
|
||||
CharArray(size) { it.toChar() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toLong() },
|
||||
LongArray(size) { it.toLong() }.scanReduce { acc, e -> acc + e }
|
||||
expected,
|
||||
ShortArray(size) { it.toShort() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toFloat() },
|
||||
FloatArray(size) { it.toFloat() }.scanReduce { acc, e -> acc + e.toInt() }
|
||||
expected,
|
||||
IntArray(size) { it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toDouble() },
|
||||
DoubleArray(size) { it.toDouble() }.scanReduce { acc, e -> acc + e.toInt() }
|
||||
expected,
|
||||
LongArray(size) { it.toLong() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.indices.map { it % 2 == 0 },
|
||||
BooleanArray(size) { true }.scanReduce { acc, e -> acc != e }
|
||||
expected,
|
||||
FloatArray(size) { it.toFloat() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected,
|
||||
DoubleArray(size) { it.toDouble() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.map { c -> if (c.isAsciiLetter()) c.toInt() % 2 != 0 else c }.joinToString(separator = "") },
|
||||
BooleanArray(size) { it % 2 == 0 }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduceIndexed() {
|
||||
fun runningReduce() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).take(size)
|
||||
// Array<T>
|
||||
assertEquals(
|
||||
expected,
|
||||
Array(size) { it }.runningReduce { acc, e -> acc + e }
|
||||
)
|
||||
// Primitive Arrays
|
||||
assertEquals(
|
||||
expected.map { it.toByte() },
|
||||
ByteArray(size) { it.toByte() }.runningReduce { acc, e -> (acc + e).toByte() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toChar() },
|
||||
CharArray(size) { it.toChar() }.runningReduce { acc, e -> acc + e.toInt() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toShort() },
|
||||
ShortArray(size) { it.toShort() }.runningReduce { acc, e -> (acc + e).toShort() }
|
||||
)
|
||||
assertEquals(
|
||||
expected,
|
||||
IntArray(size) { it }.runningReduce { acc, e -> acc + e }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toLong() },
|
||||
LongArray(size) { it.toLong() }.runningReduce { acc, e -> acc + e }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toFloat() },
|
||||
FloatArray(size) { it.toFloat() }.runningReduce { acc, e -> acc + e.toInt() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toDouble() },
|
||||
DoubleArray(size) { it.toDouble() }.runningReduce { acc, e -> acc + e.toInt() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.indices.map { it % 2 == 0 },
|
||||
BooleanArray(size) { true }.runningReduce { acc, e -> acc != e }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun runningReduceIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 6, 27).take(size)
|
||||
// Array<T>
|
||||
assertEquals(
|
||||
expected,
|
||||
Array(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
Array(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
)
|
||||
// Primitive Arrays
|
||||
assertEquals(
|
||||
expected.map { it.toByte() },
|
||||
ByteArray(size) { it.toByte() }.scanReduceIndexed { index, acc, e -> (index * (acc + e)).toByte() })
|
||||
ByteArray(size) { it.toByte() }.runningReduceIndexed { index, acc, e -> (index * (acc + e)).toByte() })
|
||||
assertEquals(
|
||||
expected.map { it.toChar() },
|
||||
CharArray(size) { it.toChar() }.scanReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
|
||||
CharArray(size) { it.toChar() }.runningReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toShort() },
|
||||
ShortArray(size) { it.toShort() }.scanReduceIndexed { index, acc, e -> (index * (acc + e)).toShort() }
|
||||
ShortArray(size) { it.toShort() }.runningReduceIndexed { index, acc, e -> (index * (acc + e)).toShort() }
|
||||
)
|
||||
assertEquals(
|
||||
expected,
|
||||
IntArray(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
IntArray(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toLong() },
|
||||
LongArray(size) { it.toLong() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
LongArray(size) { it.toLong() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toFloat() },
|
||||
FloatArray(size) { it.toFloat() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
FloatArray(size) { it.toFloat() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toDouble() },
|
||||
DoubleArray(size) { it.toDouble() }.scanReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
DoubleArray(size) { it.toDouble() }.runningReduceIndexed { index, acc, e -> index * (acc + e) }
|
||||
)
|
||||
assertEquals(
|
||||
expected.indices.map { it % 2 == 0 },
|
||||
BooleanArray(size) { true }.scanReduceIndexed { index, acc, e -> acc != e && index % 2 == 0 }
|
||||
BooleanArray(size) { true }.runningReduceIndexed { index, acc, e -> acc != e && index % 2 == 0 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,6 +397,7 @@ class CollectionTest {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
|
||||
assertEquals(expected, List(size) { it }.scan("") { acc, e -> acc + e })
|
||||
assertEquals(expected, List(size) { it }.runningFold("") { acc, e -> acc + e })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,22 +406,23 @@ class CollectionTest {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
|
||||
assertEquals(expected, List(size) { 'a' + it }.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
|
||||
assertEquals(expected, List(size) { 'a' + it }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduce() {
|
||||
fun runningReduce() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).take(size)
|
||||
assertEquals(expected, List(size) { it }.scanReduce { acc, e -> acc + e })
|
||||
assertEquals(expected, List(size) { it }.runningReduce { acc, e -> acc + e })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduceIndexed() {
|
||||
fun runningReduceIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 6, 27).take(size)
|
||||
assertEquals(expected, List(size) { it }.scanReduceIndexed { index, acc, e -> index * (acc + e) })
|
||||
assertEquals(expected, List(size) { it }.runningReduceIndexed { index, acc, e -> index * (acc + e) })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -166,41 +166,35 @@ public class SequenceTest {
|
||||
@Test
|
||||
fun scan() {
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
sequenceOf("", "0", "01", "012").take(size).toList(),
|
||||
indexSequence().scan("") { acc, e -> acc + e }.take(size).toList()
|
||||
)
|
||||
val expected = listOf("_", "_0", "_01", "_012").take(size)
|
||||
assertEquals(expected, indexSequence().scan("_") { acc, e -> acc + e }.take(size).toList())
|
||||
assertEquals(expected, indexSequence().runningFold("_") { acc, e -> acc + e }.take(size).toList())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
sequenceOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]").take(size).toList(),
|
||||
indexSequence().map { 'a' + it }.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList()
|
||||
)
|
||||
val source = indexSequence().map { 'a' + it }
|
||||
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]").take(size)
|
||||
assertEquals(expected, source.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList())
|
||||
assertEquals(expected, source.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" }.take(size).toList())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduce() {
|
||||
fun runningReduce() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).subList(0, size)
|
||||
assertEquals(
|
||||
sequenceOf(0, 1, 3, 6).take(size).toList(),
|
||||
indexSequence().scanReduce { acc, e -> acc + e }.take(size).toList()
|
||||
)
|
||||
assertEquals(expected, indexSequence().runningReduce { acc, e -> acc + e }.take(size).toList())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduceIndexed() {
|
||||
fun runningReduceIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
sequenceOf(0, 1, 6, 27).take(size).toList(),
|
||||
indexSequence().scanReduceIndexed { index, acc, e -> index * (acc + e) }.take(size).toList()
|
||||
)
|
||||
val expected = listOf(0, 1, 6, 27).take(size)
|
||||
assertEquals(expected, indexSequence().runningReduceIndexed { index, acc, e -> index * (acc + e) }.take(size).toList())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -674,6 +674,17 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun runningFold() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf("", "0", "01", "012", "0123").subList(0, size + 1)
|
||||
assertEquals(expected, UByteArray(size) { it.toUByte() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, UShortArray(size) { it.toUShort() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, UIntArray(size) { it.toUInt() }.runningFold("") { acc, e -> acc + e })
|
||||
assertEquals(expected, ULongArray(size) { it.toULong() }.runningFold("") { acc, e -> acc + e })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
@@ -698,47 +709,70 @@ class UnsignedArraysTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduce() {
|
||||
fun runningFoldIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).subList(0, size)
|
||||
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").subList(0, size + 1)
|
||||
assertEquals(
|
||||
expected.map { it.toUByte() },
|
||||
UByteArray(size) { it.toUByte() }.scanReduce { acc, e -> (acc + e).toUByte() }
|
||||
expected,
|
||||
UByteArray(size) { it.toUByte() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUShort() },
|
||||
UShortArray(size) { it.toUShort() }.scanReduce { acc, e -> (acc + e).toUShort() }
|
||||
expected,
|
||||
UShortArray(size) { it.toUShort() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUInt() },
|
||||
UIntArray(size) { it.toUInt() }.scanReduce { acc, e -> acc + e }
|
||||
expected,
|
||||
UIntArray(size) { it.toUInt() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toULong() },
|
||||
ULongArray(size) { it.toULong() }.scanReduce { acc, e -> acc + e }
|
||||
expected,
|
||||
ULongArray(size) { it.toULong() }.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: ${'a' + e.toInt()}]" }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduceIndexed() {
|
||||
fun runningReduce() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 3, 6).subList(0, size)
|
||||
assertEquals(
|
||||
expected.map { it.toUByte() },
|
||||
UByteArray(size) { it.toUByte() }.runningReduce { acc, e -> (acc + e).toUByte() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUShort() },
|
||||
UShortArray(size) { it.toUShort() }.runningReduce { acc, e -> (acc + e).toUShort() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUInt() },
|
||||
UIntArray(size) { it.toUInt() }.runningReduce { acc, e -> acc + e }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toULong() },
|
||||
ULongArray(size) { it.toULong() }.runningReduce { acc, e -> acc + e }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun runningReduceIndexed() {
|
||||
for (size in 0 until 4) {
|
||||
val expected = listOf(0, 1, 6, 27).subList(0, size)
|
||||
assertEquals(
|
||||
expected.map { it.toUByte() },
|
||||
UByteArray(size) { it.toUByte() }.scanReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUByte() }
|
||||
UByteArray(size) { it.toUByte() }.runningReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUByte() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUShort() },
|
||||
UShortArray(size) { it.toUShort() }.scanReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUShort() }
|
||||
UShortArray(size) { it.toUShort() }.runningReduceIndexed { index, acc, e -> (index.toUInt() * (acc + e)).toUShort() }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toUInt() },
|
||||
UIntArray(size) { it.toUInt() }.scanReduceIndexed { index, acc, e -> index.toUInt() * (acc + e) }
|
||||
UIntArray(size) { it.toUInt() }.runningReduceIndexed { index, acc, e -> index.toUInt() * (acc + e) }
|
||||
)
|
||||
assertEquals(
|
||||
expected.map { it.toULong() },
|
||||
ULongArray(size) { it.toULong() }.scanReduceIndexed { index, acc, e -> index.toULong() * (acc + e) }
|
||||
ULongArray(size) { it.toULong() }.runningReduceIndexed { index, acc, e -> index.toULong() * (acc + e) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1359,40 +1359,38 @@ class StringTest {
|
||||
@Test
|
||||
fun scan() = withOneCharSequenceArg { arg1 ->
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
listOf("", "0", "01", "012", "0123").take(size + 1),
|
||||
arg1((0 until size).joinToString(separator = "")).scan("") { acc, e -> acc + e }
|
||||
)
|
||||
val source = arg1((0 until size).joinToString(separator = ""))
|
||||
val expected = listOf("", "0", "01", "012", "0123").take(size + 1)
|
||||
assertEquals(expected, source.scan("") { acc, e -> acc + e })
|
||||
assertEquals(expected, source.runningFold("") { acc, e -> acc + e })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanIndexed() = withOneCharSequenceArg { arg1 ->
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1),
|
||||
arg1(('a' until 'a' + size).joinToString(separator = "")).scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" }
|
||||
)
|
||||
val source = arg1(('a' until 'a' + size).joinToString(separator = ""))
|
||||
val expected = listOf("+", "+[0: a]", "+[0: a][1: b]", "+[0: a][1: b][2: c]", "+[0: a][1: b][2: c][3: d]").take(size + 1)
|
||||
assertEquals(expected, source.scanIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
|
||||
assertEquals(expected, source.runningFoldIndexed("+") { index, acc, e -> "$acc[$index: $e]" })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduce() = withOneCharSequenceArg { arg1 ->
|
||||
fun runningReduce() = withOneCharSequenceArg { arg1 ->
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
listOf(0, 1, 3, 6).take(size).map { it.toChar() },
|
||||
arg1((0.toChar() until size.toChar()).joinToString(separator = "")).scanReduce { acc, e -> acc + e.toInt() }
|
||||
)
|
||||
val expected = listOf(0, 1, 3, 6).take(size).map { it.toChar() }
|
||||
val source = arg1((0.toChar() until size.toChar()).joinToString(separator = ""))
|
||||
assertEquals(expected, source.runningReduce { acc, e -> acc + e.toInt() })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scanReduceIndexed() = withOneCharSequenceArg { arg1 ->
|
||||
for (size in 0 until 4) {
|
||||
assertEquals(
|
||||
listOf(0, 1, 6, 27).take(size).map { it.toChar() },
|
||||
arg1((0.toChar() until size.toChar()).joinToString(separator = "")).scanReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() }
|
||||
)
|
||||
val expected = listOf(0, 1, 6, 27).take(size).map { it.toChar() }
|
||||
val source = arg1((0.toChar() until size.toChar()).joinToString(separator = ""))
|
||||
assertEquals(expected, source.runningReduceIndexed { index, acc, e -> (index * (acc.toInt() + e.toInt())).toChar() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
@@ -1575,6 +1575,10 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun reversedArray ([Ljava/lang/Object;)[Ljava/lang/Object;
|
||||
public static final fun reversedArray ([S)[S
|
||||
public static final fun reversedArray ([Z)[Z
|
||||
public static final fun runningFold ([Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningFoldIndexed ([Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun runningReduce ([Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningReduceIndexed ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scan ([Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun scanIndexed ([Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scanReduce ([Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
@@ -2192,6 +2196,10 @@ public final class kotlin/collections/CollectionsKt {
|
||||
public static final fun retainAll (Ljava/util/List;Lkotlin/jvm/functions/Function1;)Z
|
||||
public static final fun reverse (Ljava/util/List;)V
|
||||
public static final fun reversed (Ljava/lang/Iterable;)Ljava/util/List;
|
||||
public static final fun runningFold (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningFoldIndexed (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun runningReduce (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningReduceIndexed (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scan (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun scanIndexed (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scanReduce (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
@@ -4742,6 +4750,10 @@ public final class kotlin/sequences/SequencesKt {
|
||||
public static final fun reduceIndexedOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun requireNoNulls (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
|
||||
public static final fun runningFold (Lkotlin/sequences/Sequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
|
||||
public static final fun runningFoldIndexed (Lkotlin/sequences/Sequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlin/sequences/Sequence;
|
||||
public static final fun runningReduce (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
|
||||
public static final fun runningReduceIndexed (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function3;)Lkotlin/sequences/Sequence;
|
||||
public static final fun scan (Lkotlin/sequences/Sequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
|
||||
public static final fun scanIndexed (Lkotlin/sequences/Sequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlin/sequences/Sequence;
|
||||
public static final fun scanReduce (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
|
||||
@@ -5193,6 +5205,10 @@ public final class kotlin/text/StringsKt {
|
||||
public static final fun replaceRange (Ljava/lang/CharSequence;IILjava/lang/CharSequence;)Ljava/lang/CharSequence;
|
||||
public static final fun replaceRange (Ljava/lang/CharSequence;Lkotlin/ranges/IntRange;Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
|
||||
public static final fun reversed (Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
|
||||
public static final fun runningFold (Ljava/lang/CharSequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningFoldIndexed (Ljava/lang/CharSequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun runningReduce (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun runningReduceIndexed (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scan (Ljava/lang/CharSequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
public static final fun scanIndexed (Ljava/lang/CharSequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
|
||||
public static final fun scanReduce (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
|
||||
|
||||
@@ -1215,12 +1215,11 @@ object Aggregates : TemplateGroupBase() {
|
||||
$initialValueRequirement"""
|
||||
}
|
||||
|
||||
val f_scan = fn("scan(initial: R, operation: (acc: R, T) -> R)") {
|
||||
val f_runningFold = fn("runningFold(initial: R, operation: (acc: R, T) -> R)") {
|
||||
includeDefault()
|
||||
include(CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
since("1.4")
|
||||
|
||||
specialFor(Iterables, ArraysOfObjects, CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
@@ -1238,7 +1237,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
@param [operation] function that takes current accumulator value and ${f.element.prefixWithArticle()}, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scan")
|
||||
sample("samples.collections.Collections.Aggregates.runningFold")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences) {
|
||||
@@ -1273,7 +1272,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
return sequence {
|
||||
yield(initial)
|
||||
var accumulator = initial
|
||||
for (element in this@scan) {
|
||||
for (element in this@runningFold) {
|
||||
accumulator = operation(accumulator, element)
|
||||
yield(accumulator)
|
||||
}
|
||||
@@ -1282,7 +1281,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_scanIndexed = fn("scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") {
|
||||
val f_scan = fn("scan(initial: R, operation: (acc: R, T) -> R)") {
|
||||
includeDefault()
|
||||
include(CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
@@ -1297,6 +1296,34 @@ object Aggregates : TemplateGroupBase() {
|
||||
returns("List<R>")
|
||||
specialFor(Sequences) { returns("Sequence<R>") }
|
||||
|
||||
doc {
|
||||
"""
|
||||
Returns a ${f.mapResult} containing successive accumulation values generated by applying [operation] from left to right
|
||||
to each ${f.element} and current accumulator value that starts with [initial] value.
|
||||
${scanAccMutationNote(true, f)}
|
||||
@param [operation] function that takes current accumulator value and ${f.element.prefixWithArticle()}, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scan")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body { "return runningFold(initial, operation)" }
|
||||
}
|
||||
|
||||
val f_runningFoldIndexed = fn("runningFoldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") {
|
||||
includeDefault()
|
||||
include(CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
since("1.4")
|
||||
|
||||
specialFor(Iterables, ArraysOfObjects, CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
typeParam("R")
|
||||
|
||||
returns("List<R>")
|
||||
specialFor(Sequences) { returns("Sequence<R>") }
|
||||
|
||||
doc {
|
||||
"""
|
||||
Returns a ${f.mapResult} containing successive accumulation values generated by applying [operation] from left to right
|
||||
@@ -1306,7 +1333,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
and the ${f.element} itself, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scan")
|
||||
sample("samples.collections.Collections.Aggregates.runningFold")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences) {
|
||||
@@ -1343,7 +1370,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
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)
|
||||
}
|
||||
@@ -1352,12 +1379,41 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_scanReduce = fn("scanReduce(operation: (acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
val f_scanIndexed = fn("scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") {
|
||||
includeDefault()
|
||||
include(CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
|
||||
specialFor(Iterables, ArraysOfObjects, CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
typeParam("R")
|
||||
|
||||
returns("List<R>")
|
||||
specialFor(Sequences) { returns("Sequence<R>") }
|
||||
|
||||
doc {
|
||||
"""
|
||||
Returns a ${f.mapResult} containing successive accumulation values generated by applying [operation] from left to right
|
||||
to each ${f.element}, its index in the original ${f.collection} and current accumulator value that starts with [initial] value.
|
||||
${scanAccMutationNote(true, f)}
|
||||
@param [operation] function that takes the index of ${f.element.prefixWithArticle()}, current accumulator value
|
||||
and the ${f.element} itself, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scan")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body { "return runningFoldIndexed(initial, operation)" }
|
||||
}
|
||||
|
||||
val f_runningReduce = fn("runningReduce(operation: (acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
since("1.4")
|
||||
|
||||
specialFor(CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
@@ -1371,7 +1427,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
@param [operation] function that takes current accumulator value and ${f.element.prefixWithArticle()}, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scanReduce")
|
||||
sample("samples.collections.Collections.Aggregates.runningReduce")
|
||||
|
||||
body {
|
||||
"""
|
||||
@@ -1388,11 +1444,10 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_scanReduceIndexed = fn("scanReduceIndexed(operation: (index: Int, acc: T, T) -> T)") {
|
||||
val f_runningReduceIndexed = fn("runningReduceIndexed(operation: (index: Int, acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
since("1.4")
|
||||
|
||||
specialFor(CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
@@ -1408,7 +1463,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
and the ${f.element} itself, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scanReduce")
|
||||
sample("samples.collections.Collections.Aggregates.runningReduce")
|
||||
|
||||
body {
|
||||
"""
|
||||
@@ -1425,7 +1480,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_scanReduceSuper = fn("scanReduce(operation: (acc: S, T) -> S)") {
|
||||
val f_runningReduceSuper = fn("runningReduce(operation: (acc: S, T) -> S)") {
|
||||
include(ArraysOfObjects, Iterables, Sequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
@@ -1447,7 +1502,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
@param [operation] function that takes current accumulator value and the ${f.element}, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scanReduce")
|
||||
sample("samples.collections.Collections.Aggregates.runningReduce")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body(ArraysOfObjects) {
|
||||
@@ -1494,11 +1549,10 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_scanReduceIndexedSuper = fn("scanReduceIndexed(operation: (index: Int, acc: S, T) -> S)") {
|
||||
val f_runningReduceIndexedSuper = fn("runningReduceIndexed(operation: (index: Int, acc: S, T) -> S)") {
|
||||
include(ArraysOfObjects, Iterables, Sequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
since("1.4")
|
||||
|
||||
specialFor(ArraysOfObjects, Iterables) { inline() }
|
||||
|
||||
@@ -1517,7 +1571,7 @@ object Aggregates : TemplateGroupBase() {
|
||||
and the ${f.element} itself, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.scanReduce")
|
||||
sample("samples.collections.Collections.Aggregates.runningReduce")
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
body(ArraysOfObjects) {
|
||||
@@ -1566,6 +1620,73 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val f_scanReduce = fn("scanReduce(operation: (acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
deprecate(Deprecation("Use runningReduce instead.", "runningReduce(operation)", DeprecationLevel.WARNING))
|
||||
|
||||
specialFor(CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
returns("List<T>")
|
||||
|
||||
body { "return runningReduce(operation)" }
|
||||
}
|
||||
|
||||
val f_scanReduceIndexed = fn("scanReduceIndexed(operation: (index: Int, acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
deprecate(Deprecation("Use runningReduceIndexed instead.", "runningReduceIndexed(operation)", DeprecationLevel.WARNING))
|
||||
|
||||
specialFor(CharSequences) { inline() }
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
returns("List<T>")
|
||||
|
||||
body { "return runningReduceIndexed(operation)" }
|
||||
}
|
||||
|
||||
val f_scanReduceSuper = fn("scanReduce(operation: (acc: S, T) -> S)") {
|
||||
include(ArraysOfObjects, Iterables, Sequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
deprecate(Deprecation("Use runningReduce instead.", "runningReduce(operation)", DeprecationLevel.WARNING))
|
||||
|
||||
specialFor(ArraysOfObjects, Iterables) { inline() }
|
||||
|
||||
typeParam("S")
|
||||
typeParam("T : S")
|
||||
|
||||
returns("List<S>")
|
||||
specialFor(Sequences) { returns("Sequence<S>") }
|
||||
body { "return runningReduce(operation)" }
|
||||
}
|
||||
|
||||
val f_scanReduceIndexedSuper = fn("scanReduceIndexed(operation: (index: Int, acc: S, T) -> S)") {
|
||||
include(ArraysOfObjects, Iterables, Sequences)
|
||||
} builder {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
deprecate(Deprecation("Use runningReduceIndexed instead.", "runningReduceIndexed(operation)", DeprecationLevel.WARNING))
|
||||
|
||||
specialFor(ArraysOfObjects, Iterables) { inline() }
|
||||
|
||||
typeParam("S")
|
||||
typeParam("T : S")
|
||||
|
||||
returns("List<S>")
|
||||
specialFor(Sequences) { returns("Sequence<S>") }
|
||||
|
||||
body { "return runningReduceIndexed(operation)" }
|
||||
|
||||
}
|
||||
|
||||
val f_onEach = fn("onEach(action: (T) -> Unit)") {
|
||||
includeDefault()
|
||||
include(Maps, CharSequences, ArraysOfUnsigned)
|
||||
|
||||
Reference in New Issue
Block a user