Scan functions for Sequences and Iterable #KT-7657
This commit is contained in:
@@ -13568,6 +13568,872 @@ public inline fun CharArray.reduceRightOrNull(operation: (Char, acc: Char) -> Ch
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> Array<out T>.scan(initial: R, operation: (acc: R, T) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> ByteArray.scan(initial: R, operation: (acc: R, Byte) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> ShortArray.scan(initial: R, operation: (acc: R, Short) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> IntArray.scan(initial: R, operation: (acc: R, Int) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> LongArray.scan(initial: R, operation: (acc: R, Long) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> FloatArray.scan(initial: R, operation: (acc: R, Float) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> DoubleArray.scan(initial: R, operation: (acc: R, Double) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> BooleanArray.scan(initial: R, operation: (acc: R, Boolean) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> CharArray.scan(initial: R, operation: (acc: R, Char) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <T, R> Array<out T>.scanIndexed(initial: R, operation: (index: Int, acc: R, T) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> ByteArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Byte) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> ShortArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Short) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> IntArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Int) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> LongArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Long) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> FloatArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Float) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> DoubleArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Double) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> BooleanArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Boolean) -> 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R> CharArray.scanIndexed(initial: R, operation: (index: Int, acc: R, Char) -> 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 the element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <S, T : S> Array<out T>.scanReduce(operation: (acc: S, T) -> S): List<S> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator: S = this[0]
|
||||
val result = ArrayList<S>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.scanReduce(operation: (acc: Byte, Byte) -> Byte): List<Byte> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Byte>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.scanReduce(operation: (acc: Short, Short) -> Short): List<Short> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Short>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.scanReduce(operation: (acc: Int, Int) -> Int): List<Int> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Int>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.scanReduce(operation: (acc: Long, Long) -> Long): List<Long> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Long>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.scanReduce(operation: (acc: Float, Float) -> Float): List<Float> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Float>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.scanReduce(operation: (acc: Double, Double) -> Double): List<Double> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Double>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.scanReduce(operation: (acc: Boolean, Boolean) -> Boolean): List<Boolean> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Boolean>(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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.scanReduce(operation: (acc: Char, Char) -> Char): List<Char> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Char>(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.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <S, T : S> Array<out T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator: S = this[0]
|
||||
val result = ArrayList<S>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.scanReduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): List<Byte> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Byte>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.scanReduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): List<Short> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Short>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.scanReduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): List<Int> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Int>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.scanReduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): List<Long> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Long>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.scanReduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): List<Float> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Float>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.scanReduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): List<Double> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Double>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.scanReduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): List<Boolean> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Boolean>(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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.scanReduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): List<Char> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Char>(size).apply { add(accumulator) }
|
||||
for (index in 1 until size) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each element in the array.
|
||||
*/
|
||||
|
||||
@@ -1962,6 +1962,110 @@ public inline fun <S, T : S> List<T>.reduceRightOrNull(operation: (T, acc: S) ->
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
val estimatedSize = collectionSizeOrDefault(9)
|
||||
if (estimatedSize == 0) return listOf(initial)
|
||||
val result = ArrayList<R>(estimatedSize + 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 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> {
|
||||
val estimatedSize = collectionSizeOrDefault(9)
|
||||
if (estimatedSize == 0) return listOf(initial)
|
||||
val result = ArrayList<R>(estimatedSize + 1).apply { add(initial) }
|
||||
var index = 0
|
||||
var accumulator = initial
|
||||
for (element in this) {
|
||||
accumulator = operation(index++, 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 the first element of this collection.
|
||||
*
|
||||
* 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 the element, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <S, T : S> Iterable<T>.scanReduce(operation: (acc: S, T) -> S): List<S> {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) return emptyList()
|
||||
var accumulator: S = iterator.next()
|
||||
val result = ArrayList<S>(collectionSizeOrDefault(10)).apply { add(accumulator) }
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(accumulator, iterator.next())
|
||||
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 collection and current accumulator value that starts with the first element of this collection.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <S, T : S> Iterable<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S> {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) return emptyList()
|
||||
var accumulator: S = iterator.next()
|
||||
val result = ArrayList<S>(collectionSizeOrDefault(10)).apply { add(accumulator) }
|
||||
var index = 1
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(index++, accumulator, iterator.next())
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each element in the collection.
|
||||
*/
|
||||
|
||||
@@ -1405,6 +1405,124 @@ public inline fun <S, T : S> Sequence<T>.reduceOrNull(operation: (acc: S, T) ->
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 sequence {
|
||||
yield(initial)
|
||||
var accumulator = initial
|
||||
for (element in this@scan) {
|
||||
accumulator = operation(accumulator, element)
|
||||
yield(accumulator)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 sequence {
|
||||
yield(initial)
|
||||
var index = 0
|
||||
var accumulator = initial
|
||||
for (element in this@scanIndexed) {
|
||||
accumulator = operation(checkIndexOverflow(index++), accumulator, element)
|
||||
yield(accumulator)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the first element of this sequence.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting sequence.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and the element, and calculates the next accumulator value.
|
||||
*
|
||||
* The operation is _intermediate_ and _stateless_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun <S, T : S> Sequence<T>.scanReduce(operation: (acc: S, T) -> S): Sequence<S> {
|
||||
return sequence {
|
||||
val iterator = iterator()
|
||||
if (iterator.hasNext()) {
|
||||
var accumulator: S = iterator.next()
|
||||
yield(accumulator)
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(accumulator, iterator.next())
|
||||
yield(accumulator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the first element of this sequence.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting sequence.
|
||||
*
|
||||
* @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.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun <S, T : S> Sequence<T>.scanReduceIndexed(operation: (index: Int, acc: S, T) -> S): Sequence<S> {
|
||||
return sequence {
|
||||
val iterator = iterator()
|
||||
if (iterator.hasNext()) {
|
||||
var accumulator: S = iterator.next()
|
||||
yield(accumulator)
|
||||
var index = 1
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(checkIndexOverflow(index++), accumulator, iterator.next())
|
||||
yield(accumulator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each element in the sequence.
|
||||
*
|
||||
|
||||
@@ -1288,6 +1288,104 @@ public inline fun CharSequence.reduceRightOrNull(operation: (Char, acc: Char) ->
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
|
||||
* to each character and current accumulator value that starts with [initial] value.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting list.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <R> CharSequence.scan(initial: R, operation: (acc: R, Char) -> R): List<R> {
|
||||
if (isEmpty()) return listOf(initial)
|
||||
val result = ArrayList<R>(length + 1).apply { add(initial) }
|
||||
var accumulator = initial
|
||||
for (element in this) {
|
||||
accumulator = operation(accumulator, element)
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
|
||||
* to each character, its index in the original char sequence and current accumulator value that starts with [initial] value.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting list.
|
||||
*
|
||||
* @param [operation] function that takes the index of a character, current accumulator value
|
||||
* and the character itself, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <R> CharSequence.scanIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): List<R> {
|
||||
if (isEmpty()) return listOf(initial)
|
||||
val result = ArrayList<R>(length + 1).apply { add(initial) }
|
||||
var accumulator = initial
|
||||
for (index in indices) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
|
||||
* to each character and current accumulator value that starts with the first character of this char sequence.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting list.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun CharSequence.scanReduce(operation: (acc: Char, Char) -> Char): List<Char> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Char>(length).apply { add(accumulator) }
|
||||
for (index in 1 until length) {
|
||||
accumulator = operation(accumulator, this[index])
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
|
||||
* to each character, its index in the original char sequence and current accumulator value that starts with the first character of this char sequence.
|
||||
*
|
||||
* Note that `acc` value passed to [operation] function should not be mutated;
|
||||
* otherwise it would affect the previous value in resulting list.
|
||||
*
|
||||
* @param [operation] function that takes the index of a character, current accumulator value
|
||||
* and the character itself, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.scanReduce
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun CharSequence.scanReduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): List<Char> {
|
||||
if (isEmpty()) return emptyList()
|
||||
var accumulator = this[0]
|
||||
val result = ArrayList<Char>(length).apply { add(accumulator) }
|
||||
for (index in 1 until length) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
result.add(accumulator)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each character in the char sequence.
|
||||
*/
|
||||
|
||||
@@ -6038,6 +6038,430 @@ public inline fun UShortArray.reduceRightOrNull(operation: (UShort, acc: UShort)
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> 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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> 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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.scan
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each element in the array.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user