Implement reduceIndexedOrNull and reduceRightIndexedOrNull #KT-36866
This commit is contained in:
@@ -13562,6 +13562,186 @@ public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Cha
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun <S, T : S> Array<out T>.reduceIndexedOrNull(operation: (index: Int, acc: S, T) -> S): S? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator: S = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun ByteArray.reduceIndexedOrNull(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun ShortArray.reduceIndexedOrNull(operation: (index: Int, acc: Short, Short) -> Short): Short? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun IntArray.reduceIndexedOrNull(operation: (index: Int, acc: Int, Int) -> Int): Int? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun LongArray.reduceIndexedOrNull(operation: (index: Int, acc: Long, Long) -> Long): Long? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun FloatArray.reduceIndexedOrNull(operation: (index: Int, acc: Float, Float) -> Float): Float? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun DoubleArray.reduceIndexedOrNull(operation: (index: Int, acc: Double, Double) -> Double): Double? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun BooleanArray.reduceIndexedOrNull(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun CharArray.reduceIndexedOrNull(operation: (index: Int, acc: Char, Char) -> Char): Char? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. Returns null if the array is empty.
|
||||
*
|
||||
@@ -14021,6 +14201,195 @@ public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun <S, T : S> Array<out T>.reduceRightIndexedOrNull(operation: (index: Int, T, acc: S) -> S): S? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator: S = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun ByteArray.reduceRightIndexedOrNull(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun ShortArray.reduceRightIndexedOrNull(operation: (index: Int, Short, acc: Short) -> Short): Short? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun IntArray.reduceRightIndexedOrNull(operation: (index: Int, Int, acc: Int) -> Int): Int? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun LongArray.reduceRightIndexedOrNull(operation: (index: Int, Long, acc: Long) -> Long): Long? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun FloatArray.reduceRightIndexedOrNull(operation: (index: Int, Float, acc: Float) -> Float): Float? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun DoubleArray.reduceRightIndexedOrNull(operation: (index: Int, Double, acc: Double) -> Double): Double? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun BooleanArray.reduceRightIndexedOrNull(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun CharArray.reduceRightIndexedOrNull(operation: (index: Int, Char, acc: Char) -> Char): Char? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. Returns null if the array is empty.
|
||||
*
|
||||
|
||||
@@ -1893,6 +1893,27 @@ public inline fun <S, T : S> Iterable<T>.reduceIndexed(operation: (index: Int, a
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original collection.
|
||||
* Returns null if the collection is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun <S, T : S> Iterable<T>.reduceIndexedOrNull(operation: (index: Int, acc: S, T) -> S): S? {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
var index = 1
|
||||
var accumulator: S = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(checkIndexOverflow(index++), accumulator, iterator.next())
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. Returns null if the collection is empty.
|
||||
*
|
||||
@@ -1946,6 +1967,28 @@ public inline fun <S, T : S> List<T>.reduceRightIndexed(operation: (index: Int,
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original list and current accumulator value.
|
||||
* Returns null if the list is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun <S, T : S> List<T>.reduceRightIndexedOrNull(operation: (index: Int, T, acc: S) -> S): S? {
|
||||
val iterator = listIterator(size)
|
||||
if (!iterator.hasPrevious())
|
||||
return null
|
||||
var accumulator: S = iterator.previous()
|
||||
while (iterator.hasPrevious()) {
|
||||
val index = iterator.previousIndex()
|
||||
accumulator = operation(index, iterator.previous(), accumulator)
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. Returns null if the list is empty.
|
||||
*
|
||||
|
||||
@@ -1388,6 +1388,29 @@ public inline fun <S, T : S> Sequence<T>.reduceIndexed(operation: (index: Int, a
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original sequence.
|
||||
* Returns null if the sequence is empty.
|
||||
* @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 _terminal_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun <S, T : S> Sequence<T>.reduceIndexedOrNull(operation: (index: Int, acc: S, T) -> S): S? {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
var index = 1
|
||||
var accumulator: S = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(checkIndexOverflow(index++), accumulator, iterator.next())
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. Returns null if the sequence is empty.
|
||||
*
|
||||
|
||||
@@ -1220,6 +1220,26 @@ public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char,
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first character and applying [operation] from left to right
|
||||
* to current accumulator value and each character with its index in the original char sequence.
|
||||
* Returns null if the char sequence is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun CharSequence.reduceIndexedOrNull(operation: (index: Int, acc: Char, Char) -> Char): Char? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. Returns null if the char sequence is empty.
|
||||
*
|
||||
@@ -1271,6 +1291,27 @@ public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char,
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last character and applying [operation] from right to left
|
||||
* to each character with its index in the original char sequence and current accumulator value.
|
||||
* Returns null if the char sequence is empty.
|
||||
* @param [operation] function that takes the index of a character, the character itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public inline fun CharSequence.reduceRightIndexedOrNull(operation: (index: Int, Char, acc: Char) -> Char): Char? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value. Returns null if the char sequence is empty.
|
||||
*
|
||||
|
||||
@@ -6002,6 +6002,94 @@ public inline fun UShortArray.reduceIndexed(operation: (index: Int, acc: UShort,
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.reduceIndexedOrNull(operation: (index: Int, acc: UInt, UInt) -> UInt): UInt? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.reduceIndexedOrNull(operation: (index: Int, acc: ULong, ULong) -> ULong): ULong? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.reduceIndexedOrNull(operation: (index: Int, acc: UByte, UByte) -> UByte): UByte? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original array.
|
||||
* Returns null if the array is empty.
|
||||
* @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.reduceOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.reduceIndexedOrNull(operation: (index: Int, acc: UShort, UShort) -> UShort): UShort? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. Returns null if the array is empty.
|
||||
*
|
||||
@@ -6238,6 +6326,98 @@ public inline fun UShortArray.reduceRightIndexed(operation: (index: Int, UShort,
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.reduceRightIndexedOrNull(operation: (index: Int, UInt, acc: UInt) -> UInt): UInt? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.reduceRightIndexedOrNull(operation: (index: Int, ULong, acc: ULong) -> ULong): ULong? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.reduceRightIndexedOrNull(operation: (index: Int, UByte, acc: UByte) -> UByte): UByte? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original array and current accumulator value.
|
||||
* Returns null if the array is empty.
|
||||
* @param [operation] function that takes the index of an element, the element itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.reduceRightIndexedOrNull(operation: (index: Int, UShort, acc: UShort) -> UShort): UShort? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. Returns null if the array is empty.
|
||||
*
|
||||
|
||||
@@ -698,6 +698,7 @@ class Collections {
|
||||
fun reduceOrNull() {
|
||||
val strings = listOf("a", "b", "c", "d")
|
||||
assertPrints(strings.reduceOrNull { acc, string -> acc + string }, "abcd")
|
||||
assertPrints(strings.reduceIndexedOrNull { index, acc, string -> acc + string + index }, "ab1c2d3")
|
||||
|
||||
assertPrints(emptyList<String>().reduceOrNull { _, _ -> "" }, "null")
|
||||
}
|
||||
@@ -706,6 +707,7 @@ class Collections {
|
||||
fun reduceRightOrNull() {
|
||||
val strings = listOf("a", "b", "c", "d")
|
||||
assertPrints(strings.reduceRightOrNull { string, acc -> acc + string }, "dcba")
|
||||
assertPrints(strings.reduceRightIndexedOrNull { index, string, acc -> acc + string + index }, "dc2b1a0")
|
||||
|
||||
assertPrints(emptyList<String>().reduceRightOrNull { _, _ -> "" }, "null")
|
||||
}
|
||||
|
||||
@@ -1016,6 +1016,20 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNull() {
|
||||
expect(-1) { intArrayOf(1, 2, 3).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1.toLong()) { longArrayOf(1, 2, 3).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1F) { floatArrayOf(1F, 2F, 3F).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceIndexedOrNull { index, a, b -> if (a > b && index == 1) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceIndexedOrNull { index, a, b -> a && b || index == 2 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceIndexedOrNull { index, a, b -> a && b && index != 1 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceIndexedOrNull { index, a, b -> if (index != 2) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceIndexedOrNull { index, a, b -> if (index != 2) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
expect(null, { intArrayOf().reduceIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexed() {
|
||||
expect(1) { intArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
expect(1.toLong()) { longArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
@@ -1032,6 +1046,20 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexedOrNull() {
|
||||
expect(1) { intArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1.toLong()) { longArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1F) { floatArrayOf(1F, 2F, 3F).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceRightIndexedOrNull { index, a, b -> if (a > b && index == 0) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceRightIndexedOrNull { index, a, b -> a && b || index == 1 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceRightIndexedOrNull { index, a, b -> a && b && index != 0 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, a, b -> if (index != 1) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, a, b -> if (index != 1) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
expect(null, { intArrayOf().reduceRightIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduce() {
|
||||
expect(-4) { intArrayOf(1, 2, 3).reduce { a, b -> a - b } }
|
||||
expect(-4.toLong()) { longArrayOf(1, 2, 3).reduce { a, b -> a - b } }
|
||||
|
||||
@@ -296,6 +296,24 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNull() {
|
||||
expect("123") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduceIndexedOrNull { index, a, b -> if (index == 3) a else a + b }
|
||||
}
|
||||
|
||||
expect(5) {
|
||||
listOf(2, 3).reduceIndexedOrNull { index, acc: Number, e ->
|
||||
assertEquals(1, index)
|
||||
assertEquals(2, acc)
|
||||
assertEquals(3, e)
|
||||
acc.toInt() + e
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arrayListOf<Int>().reduceIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexed() {
|
||||
expect("234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
@@ -316,6 +334,24 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexedOrNull() {
|
||||
expect("234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduceRightIndexedOrNull { index, a, b -> if (index == 0) b else a + b }
|
||||
}
|
||||
|
||||
expect(1) {
|
||||
listOf(2, 3).reduceRightIndexedOrNull { index, e, acc: Number ->
|
||||
assertEquals(0, index)
|
||||
assertEquals(3, acc)
|
||||
assertEquals(2, e)
|
||||
acc.toInt() - e
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arrayListOf<Int>().reduceRightIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduce() {
|
||||
expect("1234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
|
||||
@@ -426,6 +426,10 @@ public class SequenceTest {
|
||||
expect(null, { sequenceOf<Int>().reduceOrNull { acc, i -> acc + i } })
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNullOnEmpty() {
|
||||
expect(null, { sequenceOf<Int>().reduceIndexedOrNull { index, acc, i -> acc + i + index } })
|
||||
}
|
||||
|
||||
@Test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
|
||||
@Test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
|
||||
@Test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
|
||||
|
||||
@@ -548,6 +548,16 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceIndexedOrNull() {
|
||||
expect(1u) { ubyteArrayOf(3, 2, 1).reduceIndexedOrNull { index, acc, e -> if (index != 2) (e - acc).toUByte() else e } }
|
||||
expect(1u) { ushortArrayOf(3, 2, 1).reduceIndexedOrNull { index, acc, e -> if (index != 2) (e - acc).toUShort() else e } }
|
||||
expect(UInt.MAX_VALUE) { uintArrayOf(1, 2, 3).reduceIndexedOrNull { index, acc, e -> index.toUInt() + acc - e } }
|
||||
expect(ULong.MAX_VALUE) { ulongArrayOf(1, 2, 3).reduceIndexedOrNull { index, acc, e -> index.toULong() + acc - e } }
|
||||
|
||||
expect(null, { uintArrayOf().reduceIndexedOrNull { index, acc, e -> index.toUInt() + e + acc } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceRight() {
|
||||
expect(2u) { ubyteArrayOf(1, 2, 3).reduceRightOrNull { e, acc -> (e - acc).toUByte() } }
|
||||
@@ -582,6 +592,16 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceRightIndexedOrNull() {
|
||||
expect(1u) { ubyteArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, e, acc -> if (index != 1) (e - acc).toUByte() else e } }
|
||||
expect(1u) { ushortArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, e, acc -> if (index != 1) (e - acc).toUShort() else e } }
|
||||
expect(1u) { uintArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, e, acc -> index.toUInt() + e - acc } }
|
||||
expect(1uL) { ulongArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, e, acc -> index.toULong() + e - acc } }
|
||||
|
||||
expect(null, { uintArrayOf().reduceRightIndexedOrNull { index, e, acc -> index.toUInt() + e + acc } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun forEach() {
|
||||
var i = 0
|
||||
|
||||
@@ -1268,6 +1268,22 @@ class StringTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNull() = withOneCharSequenceArg { arg1 ->
|
||||
// get the 3rd character
|
||||
assertEquals('c', arg1("bacfd").reduceIndexedOrNull { index, v, c -> if (index == 2) c else v })
|
||||
|
||||
expect('c') {
|
||||
"ab".reduceIndexedOrNull { index, acc, e ->
|
||||
assertEquals(1, index)
|
||||
assertEquals('a', acc)
|
||||
assertEquals('b', e)
|
||||
e + (e - acc)
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arg1("").reduceIndexedOrNull { _, _, _ -> '\n' } })
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexed() = withOneCharSequenceArg { arg1 ->
|
||||
// get the 3rd character
|
||||
assertEquals('c', arg1("bacfd").reduceRightIndexed { index, c, v -> if (index == 2) c else v })
|
||||
@@ -1286,6 +1302,22 @@ class StringTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexedOrNull() = withOneCharSequenceArg { arg1 ->
|
||||
// get the 3rd character
|
||||
assertEquals('c', arg1("bacfd").reduceRightIndexedOrNull { index, c, v -> if (index == 2) c else v })
|
||||
|
||||
expect('c') {
|
||||
"ab".reduceRightIndexedOrNull { index, e, acc ->
|
||||
assertEquals(0, index)
|
||||
assertEquals('b', acc)
|
||||
assertEquals('a', e)
|
||||
acc + (acc - e)
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arg1("").reduceRightIndexedOrNull { _, _, _ -> '\n' } })
|
||||
}
|
||||
|
||||
@Test fun reduce() = withOneCharSequenceArg { arg1 ->
|
||||
// get the smallest character(by char value)
|
||||
assertEquals('a', arg1("bacfd").reduce { v, c -> if (v > c) c else v })
|
||||
|
||||
Reference in New Issue
Block a user