From d88fd8aa6f0865b59f84fd28a8b77487b7712da1 Mon Sep 17 00:00:00 2001 From: Karen Schwane Date: Wed, 31 Oct 2018 08:21:30 +0100 Subject: [PATCH] KT-20357 Add samples for maxBy and minBy --- .../stdlib/common/src/generated/_Arrays.kt | 36 +++++++++++++++++++ .../common/src/generated/_Collections.kt | 4 +++ .../stdlib/common/src/generated/_Maps.kt | 4 +++ .../stdlib/common/src/generated/_Sequences.kt | 4 +++ .../stdlib/common/src/generated/_Strings.kt | 4 +++ .../test/samples/collections/collections.kt | 22 ++++++++++++ .../src/templates/Aggregates.kt | 2 ++ 7 files changed, 76 insertions(+) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index dbcdef07091..45fd9f66250 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -10960,6 +10960,8 @@ public fun CharArray.max(): Char? { /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > Array.maxBy(selector: (T) -> R): T? { if (isEmpty()) return null @@ -10978,6 +10980,8 @@ public inline fun > Array.maxBy(selector: (T) -> R): /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? { if (isEmpty()) return null @@ -10996,6 +11000,8 @@ public inline fun > ByteArray.maxBy(selector: (Byte) -> R): By /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? { if (isEmpty()) return null @@ -11014,6 +11020,8 @@ public inline fun > ShortArray.maxBy(selector: (Short) -> R): /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? { if (isEmpty()) return null @@ -11032,6 +11040,8 @@ public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > LongArray.maxBy(selector: (Long) -> R): Long? { if (isEmpty()) return null @@ -11050,6 +11060,8 @@ public inline fun > LongArray.maxBy(selector: (Long) -> R): Lo /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? { if (isEmpty()) return null @@ -11068,6 +11080,8 @@ public inline fun > FloatArray.maxBy(selector: (Float) -> R): /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? { if (isEmpty()) return null @@ -11086,6 +11100,8 @@ public inline fun > DoubleArray.maxBy(selector: (Double) -> R) /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { if (isEmpty()) return null @@ -11104,6 +11120,8 @@ public inline fun > BooleanArray.maxBy(selector: (Boolean) -> /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { if (isEmpty()) return null @@ -11387,6 +11405,8 @@ public fun CharArray.min(): Char? { /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > Array.minBy(selector: (T) -> R): T? { if (isEmpty()) return null @@ -11405,6 +11425,8 @@ public inline fun > Array.minBy(selector: (T) -> R): /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? { if (isEmpty()) return null @@ -11423,6 +11445,8 @@ public inline fun > ByteArray.minBy(selector: (Byte) -> R): By /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > ShortArray.minBy(selector: (Short) -> R): Short? { if (isEmpty()) return null @@ -11441,6 +11465,8 @@ public inline fun > ShortArray.minBy(selector: (Short) -> R): /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > IntArray.minBy(selector: (Int) -> R): Int? { if (isEmpty()) return null @@ -11459,6 +11485,8 @@ public inline fun > IntArray.minBy(selector: (Int) -> R): Int? /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > LongArray.minBy(selector: (Long) -> R): Long? { if (isEmpty()) return null @@ -11477,6 +11505,8 @@ public inline fun > LongArray.minBy(selector: (Long) -> R): Lo /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > FloatArray.minBy(selector: (Float) -> R): Float? { if (isEmpty()) return null @@ -11495,6 +11525,8 @@ public inline fun > FloatArray.minBy(selector: (Float) -> R): /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? { if (isEmpty()) return null @@ -11513,6 +11545,8 @@ public inline fun > DoubleArray.minBy(selector: (Double) -> R) /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { if (isEmpty()) return null @@ -11531,6 +11565,8 @@ public inline fun > BooleanArray.minBy(selector: (Boolean) -> /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { if (isEmpty()) return null diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 13fe1a400af..bc9aa44966a 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1622,6 +1622,8 @@ public fun > Iterable.max(): T? { /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > Iterable.maxBy(selector: (T) -> R): T? { val iterator = iterator() @@ -1707,6 +1709,8 @@ public fun > Iterable.min(): T? { /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > Iterable.minBy(selector: (T) -> R): T? { val iterator = iterator() diff --git a/libraries/stdlib/common/src/generated/_Maps.kt b/libraries/stdlib/common/src/generated/_Maps.kt index 7bde51dd3dd..d74d48efd3d 100644 --- a/libraries/stdlib/common/src/generated/_Maps.kt +++ b/libraries/stdlib/common/src/generated/_Maps.kt @@ -147,6 +147,8 @@ public inline fun Map.forEach(action: (Map.Entry) -> Unit /** * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ @kotlin.internal.InlineOnly public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { @@ -163,6 +165,8 @@ public inline fun Map.maxWith(comparator: Comparator> Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { return entries.minBy(selector) diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 28e535b03f5..71992d62698 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1141,6 +1141,8 @@ public fun > Sequence.max(): T? { * Returns the first element yielding the largest value of the given function or `null` if there are no elements. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > Sequence.maxBy(selector: (T) -> R): T? { val iterator = iterator() @@ -1236,6 +1238,8 @@ public fun > Sequence.min(): T? { * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * * The operation is _terminal_. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > Sequence.minBy(selector: (T) -> R): T? { val iterator = iterator() diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 0225f655197..3f60eef481d 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1026,6 +1026,8 @@ public fun CharSequence.max(): Char? { /** * Returns the first character yielding the largest value of the given function or `null` if there are no characters. + * + * @sample samples.collections.Collections.Aggregates.maxBy */ public inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? { if (isEmpty()) return null @@ -1070,6 +1072,8 @@ public fun CharSequence.min(): Char? { /** * Returns the first character yielding the smallest value of the given function or `null` if there are no characters. + * + * @sample samples.collections.Collections.Aggregates.minBy */ public inline fun > CharSequence.minBy(selector: (Char) -> R): Char? { if (isEmpty()) return null diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 0739d273ded..c8819de982d 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -497,6 +497,28 @@ class Collections { val emptyList = emptyList() assertFalse(emptyList.any { true }) } + + @Sample + fun maxBy() { + val nameToAge = listOf("Alice" to 42, "Bob" to 28, "Carol" to 51) + val oldestPerson = nameToAge.maxBy { it.second } + assertPrints(oldestPerson, "(Carol, 51)") + + val emptyList = emptyList>() + val emptyMax = emptyList.maxBy { it.second } + assertPrints(emptyMax, "null") + } + + @Sample + fun minBy() { + val list = listOf("abcd", "abc", "ab", "abcde") + val shortestString = list.minBy { it.length } + assertPrints(shortestString, "ab") + + val emptyList = emptyList() + val emptyMin = emptyList.minBy { it.length } + assertPrints(emptyMin, "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 2343ccaa862..a20fcf4d782 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -298,6 +298,7 @@ object Aggregates : TemplateGroupBase() { } builder { inline() doc { "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element.pluralize()}." } + sample("samples.collections.Collections.Aggregates.minBy") typeParam("R : Comparable") returns("T?") body { @@ -380,6 +381,7 @@ object Aggregates : TemplateGroupBase() { inline() doc { "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element.pluralize()}." } + sample("samples.collections.Collections.Aggregates.maxBy") typeParam("R : Comparable") returns("T?") body {