Implement reduceIndexedOrNull and reduceRightIndexedOrNull #KT-36866
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user