Add sample for reduceOrNull and reduceRightOrNull

This commit is contained in:
Alfredo Delli Bovi
2019-12-13 19:17:04 +01:00
committed by Ilya Gorbunov
parent 06008c40ab
commit 9546307243
7 changed files with 82 additions and 0 deletions
@@ -12733,6 +12733,8 @@ public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Cha
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12748,6 +12750,8 @@ public inline fun <S, T : S> Array<out T>.reduceOrNull(operation: (acc: S, T) ->
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12763,6 +12767,8 @@ public inline fun ByteArray.reduceOrNull(operation: (acc: Byte, Byte) -> Byte):
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12778,6 +12784,8 @@ public inline fun ShortArray.reduceOrNull(operation: (acc: Short, Short) -> Shor
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12793,6 +12801,8 @@ public inline fun IntArray.reduceOrNull(operation: (acc: Int, Int) -> Int): Int?
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12808,6 +12818,8 @@ public inline fun LongArray.reduceOrNull(operation: (acc: Long, Long) -> Long):
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12823,6 +12835,8 @@ public inline fun FloatArray.reduceOrNull(operation: (acc: Float, Float) -> Floa
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12838,6 +12852,8 @@ public inline fun DoubleArray.reduceOrNull(operation: (acc: Double, Double) -> D
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -12853,6 +12869,8 @@ public inline fun BooleanArray.reduceOrNull(operation: (acc: Boolean, Boolean) -
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13138,6 +13156,8 @@ public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13153,6 +13173,8 @@ public inline fun <S, T : S> Array<out T>.reduceRightOrNull(operation: (T, acc:
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13168,6 +13190,8 @@ public inline fun ByteArray.reduceRightOrNull(operation: (Byte, acc: Byte) -> By
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13183,6 +13207,8 @@ public inline fun ShortArray.reduceRightOrNull(operation: (Short, acc: Short) ->
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13198,6 +13224,8 @@ public inline fun IntArray.reduceRightOrNull(operation: (Int, acc: Int) -> Int):
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13213,6 +13241,8 @@ public inline fun LongArray.reduceRightOrNull(operation: (Long, acc: Long) -> Lo
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13228,6 +13258,8 @@ public inline fun FloatArray.reduceRightOrNull(operation: (Float, acc: Float) ->
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13243,6 +13275,8 @@ public inline fun DoubleArray.reduceRightOrNull(operation: (Double, acc: Double)
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -13258,6 +13292,8 @@ public inline fun BooleanArray.reduceRightOrNull(operation: (Boolean, acc: Boole
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -1868,6 +1868,8 @@ public inline fun <S, T : S> Iterable<T>.reduceIndexed(operation: (index: Int, a
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -1915,6 +1917,8 @@ public inline fun <S, T : S> List<T>.reduceRightIndexed(operation: (index: Int,
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -1386,6 +1386,8 @@ public inline fun <S, T : S> Sequence<T>.reduceIndexed(operation: (index: Int, a
* 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.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -1197,6 +1197,8 @@ 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. Returns null if the char sequence is empty.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -1242,6 +1244,8 @@ public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char,
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5620,6 +5620,8 @@ public inline fun UShortArray.reduceIndexed(operation: (index: Int, acc: UShort,
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5637,6 +5639,8 @@ public inline fun UIntArray.reduceOrNull(operation: (acc: UInt, UInt) -> UInt):
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5654,6 +5658,8 @@ public inline fun ULongArray.reduceOrNull(operation: (acc: ULong, ULong) -> ULon
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5671,6 +5677,8 @@ public inline fun UByteArray.reduceOrNull(operation: (acc: UByte, UByte) -> UByt
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5832,6 +5840,8 @@ public inline fun UShortArray.reduceRightIndexed(operation: (index: Int, UShort,
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5849,6 +5859,8 @@ public inline fun UIntArray.reduceRightOrNull(operation: (UInt, acc: UInt) -> UI
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5866,6 +5878,8 @@ public inline fun ULongArray.reduceRightOrNull(operation: (ULong, acc: ULong) ->
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -5883,6 +5897,8 @@ public inline fun UByteArray.reduceRightOrNull(operation: (UByte, acc: UByte) ->
/**
* 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.
*
* @sample samples.collections.Collections.Aggregates.reduceRightOrNull
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
@@ -641,6 +641,22 @@ class Collections {
val emptyMin = emptyList.minBy { it.length }
assertPrints(emptyMin, "null")
}
@Sample
fun reduceOrNull() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduceOrNull { acc, string -> acc + string }, "abcd")
assertPrints(emptyList<String>().reduceOrNull { _, _ -> "" }, "null")
}
@Sample
fun reduceRightOrNull() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduceRightOrNull { string, acc -> acc + string }, "dcba")
assertPrints(emptyList<String>().reduceRightOrNull { _, _ -> "" }, "null")
}
}
class Elements {
@@ -849,6 +849,7 @@ object Aggregates : TemplateGroupBase() {
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}. Returns null if the ${f.collection} is empty." }
sample("samples.collections.Collections.Aggregates.reduceOrNull")
returns("T?")
body {
"""
@@ -872,6 +873,7 @@ object Aggregates : TemplateGroupBase() {
inline()
doc { "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}. Returns null if the ${f.collection} is empty." }
sample("samples.collections.Collections.Aggregates.reduceOrNull")
typeParam("S")
typeParam("T : S")
returns("S?")
@@ -970,6 +972,7 @@ object Aggregates : TemplateGroupBase() {
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value. Returns null if the ${f.collection} is empty." }
sample("samples.collections.Collections.Aggregates.reduceRightOrNull")
returns("T?")
body {
"""
@@ -993,6 +996,7 @@ object Aggregates : TemplateGroupBase() {
annotation("@ExperimentalStdlibApi")
inline()
doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value. Returns null if the ${f.collection} is empty." }
sample("samples.collections.Collections.Aggregates.reduceRightOrNull")
typeParam("S")
typeParam("T : S")
returns("S?")