KT-33761 Add reduceRightOrNull
This commit is contained in:
committed by
Ilya Gorbunov
parent
f5d696d3c4
commit
06008c40ab
@@ -1240,6 +1240,21 @@ 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 and current accumulator value. Returns null if the char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun CharSequence.reduceRightOrNull(operation: (Char, acc: Char) -> Char): Char? {
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(get(index--), accumulator)
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each character in the char sequence.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user