From 9546307243d51a9ea1e1f865e5d865cd002c3ed6 Mon Sep 17 00:00:00 2001 From: Alfredo Delli Bovi Date: Fri, 13 Dec 2019 19:17:04 +0100 Subject: [PATCH] Add sample for reduceOrNull and reduceRightOrNull --- .../stdlib/common/src/generated/_Arrays.kt | 36 +++++++++++++++++++ .../common/src/generated/_Collections.kt | 4 +++ .../stdlib/common/src/generated/_Sequences.kt | 2 ++ .../stdlib/common/src/generated/_Strings.kt | 4 +++ .../stdlib/common/src/generated/_UArrays.kt | 16 +++++++++ .../test/samples/collections/collections.kt | 16 +++++++++ .../src/templates/Aggregates.kt | 4 +++ 7 files changed, 82 insertions(+) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index faa4606d02f..fb809c597f0 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -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 Array.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 Array.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 diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 060959fe08e..690c1263cb3 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1868,6 +1868,8 @@ public inline fun Iterable.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 List.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 diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index dab5480f9ff..a8abd6722c3 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1386,6 +1386,8 @@ public inline fun Sequence.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 diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 076b0e3cb74..e8888ed5538 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -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 diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 6104e9df5ff..f9074b5ae27 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -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 diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 5446428c492..9e5c4bb1dad 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -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().reduceOrNull { _, _ -> "" }, "null") + } + + @Sample + fun reduceRightOrNull() { + val strings = listOf("a", "b", "c", "d") + assertPrints(strings.reduceRightOrNull { string, acc -> acc + string }, "dcba") + + assertPrints(emptyList().reduceRightOrNull { _, _ -> "" }, "null") + } } class Elements { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 1daa8b75285..49deb32169f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -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?")