Document 'reduce' operation behavior on empty collections #KT-23322
This commit is contained in:
@@ -1006,7 +1006,12 @@ public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int {
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character.
|
||||
* Accumulates value starting with [initial] value and applying [operation] from left to right
|
||||
* to current accumulator value and each character.
|
||||
*
|
||||
* Returns the specified [initial] value if the char sequence is empty.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and a character, and calculates the next accumulator value.
|
||||
*/
|
||||
public inline fun <R> CharSequence.fold(initial: R, operation: (acc: R, Char) -> R): R {
|
||||
var accumulator = initial
|
||||
@@ -1017,6 +1022,9 @@ public inline fun <R> CharSequence.fold(initial: R, operation: (acc: R, Char) ->
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from left to right
|
||||
* to current accumulator value and each character with its index in the original char sequence.
|
||||
*
|
||||
* Returns the specified [initial] value 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.
|
||||
*/
|
||||
@@ -1028,7 +1036,12 @@ public inline fun <R> CharSequence.foldIndexed(initial: R, operation: (index: In
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value.
|
||||
* Accumulates value starting with [initial] value and applying [operation] from right to left
|
||||
* to each character and current accumulator value.
|
||||
*
|
||||
* Returns the specified [initial] value if the char sequence is empty.
|
||||
*
|
||||
* @param [operation] function that takes a character and current accumulator value, and calculates the next accumulator value.
|
||||
*/
|
||||
public inline fun <R> CharSequence.foldRight(initial: R, operation: (Char, acc: R) -> R): R {
|
||||
var index = lastIndex
|
||||
@@ -1042,6 +1055,9 @@ public inline fun <R> CharSequence.foldRight(initial: R, operation: (Char, acc:
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from right to left
|
||||
* to each character with its index in the original char sequence and current accumulator value.
|
||||
*
|
||||
* Returns the specified [initial] value 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.
|
||||
*/
|
||||
@@ -1207,7 +1223,14 @@ public inline fun <S : CharSequence> S.onEachIndexed(action: (index: Int, Char)
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character.
|
||||
* Accumulates value starting with the first character and applying [operation] from left to right
|
||||
* to current accumulator value and each character.
|
||||
*
|
||||
* Throws an exception if this char sequence is empty. If the char sequence can be empty in an expected way,
|
||||
* please use [reduceOrNull] instead. It returns `null` when its receiver is empty.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and a character,
|
||||
* and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduce
|
||||
*/
|
||||
@@ -1224,8 +1247,12 @@ public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Cha
|
||||
/**
|
||||
* 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.
|
||||
* @param [operation] function that takes the index of a character, current accumulator value
|
||||
* and the character itself and calculates the next accumulator value.
|
||||
*
|
||||
* Throws an exception if this char sequence is empty. If the char sequence can be empty in an expected way,
|
||||
* please use [reduceIndexedOrNull] instead. It returns `null` when its receiver 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.reduce
|
||||
*/
|
||||
@@ -1242,9 +1269,11 @@ public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char,
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
@@ -1260,7 +1289,13 @@ public inline fun CharSequence.reduceIndexedOrNull(operation: (index: Int, acc:
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* @param [operation] function that takes current accumulator value and a character,
|
||||
* and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceOrNull
|
||||
*/
|
||||
@@ -1277,7 +1312,14 @@ public inline fun CharSequence.reduceOrNull(operation: (acc: Char, Char) -> Char
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value.
|
||||
* Accumulates value starting with the last character and applying [operation] from right to left
|
||||
* to each character and current accumulator value.
|
||||
*
|
||||
* Throws an exception if this char sequence is empty. If the char sequence can be empty in an expected way,
|
||||
* please use [reduceRightOrNull] instead. It returns `null` when its receiver is empty.
|
||||
*
|
||||
* @param [operation] function that takes a character and current accumulator value,
|
||||
* and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRight
|
||||
*/
|
||||
@@ -1292,10 +1334,14 @@ public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char)
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last character and applying [operation] from right to left
|
||||
* Accumulates value starting with the last character and applying [operation] from right to left
|
||||
* to each character with its index in the original char sequence and current accumulator value.
|
||||
* @param [operation] function that takes the index of a character, the character itself
|
||||
* and current accumulator value, and calculates the next accumulator value.
|
||||
*
|
||||
* Throws an exception if this char sequence is empty. If the char sequence can be empty in an expected way,
|
||||
* please use [reduceRightIndexedOrNull] instead. It returns `null` when its receiver 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.reduceRight
|
||||
*/
|
||||
@@ -1311,11 +1357,13 @@ public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char,
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last character and applying [operation] from right to left
|
||||
* Accumulates value starting with the 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.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
@@ -1332,7 +1380,13 @@ public inline fun CharSequence.reduceRightIndexedOrNull(operation: (index: Int,
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Accumulates value starting with the last character and applying [operation] from right to left
|
||||
* to each character and current accumulator value.
|
||||
*
|
||||
* Returns `null` if the char sequence is empty.
|
||||
*
|
||||
* @param [operation] function that takes a character and current accumulator value,
|
||||
* and calculates the next accumulator value.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user