KT-20357 Add samples for maxBy and minBy
This commit is contained in:
@@ -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 <T, R : Comparable<R>> Array<out T>.maxBy(selector: (T) -> R): T? {
|
||||
if (isEmpty()) return null
|
||||
@@ -10978,6 +10980,8 @@ public inline fun <T, R : Comparable<R>> Array<out T>.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 <R : Comparable<R>> ByteArray.maxBy(selector: (Byte) -> R): Byte? {
|
||||
if (isEmpty()) return null
|
||||
@@ -10996,6 +11000,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> ShortArray.maxBy(selector: (Short) -> R): Short? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11014,6 +11020,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> IntArray.maxBy(selector: (Int) -> R): Int? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11032,6 +11040,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> LongArray.maxBy(selector: (Long) -> R): Long? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11050,6 +11060,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> FloatArray.maxBy(selector: (Float) -> R): Float? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11068,6 +11080,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> DoubleArray.maxBy(selector: (Double) -> R): Double? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11086,6 +11100,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11104,6 +11120,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> 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 <T, R : Comparable<R>> Array<out T>.minBy(selector: (T) -> R): T? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11405,6 +11425,8 @@ public inline fun <T, R : Comparable<R>> Array<out T>.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 <R : Comparable<R>> ByteArray.minBy(selector: (Byte) -> R): Byte? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11423,6 +11445,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> ShortArray.minBy(selector: (Short) -> R): Short? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11441,6 +11465,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> IntArray.minBy(selector: (Int) -> R): Int? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11459,6 +11485,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> LongArray.minBy(selector: (Long) -> R): Long? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11477,6 +11505,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> FloatArray.minBy(selector: (Float) -> R): Float? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11495,6 +11525,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> DoubleArray.minBy(selector: (Double) -> R): Double? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11513,6 +11545,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> BooleanArray.minBy(selector: (Boolean) -> R): Boolean? {
|
||||
if (isEmpty()) return null
|
||||
@@ -11531,6 +11565,8 @@ public inline fun <R : Comparable<R>> 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 <R : Comparable<R>> CharArray.minBy(selector: (Char) -> R): Char? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
@@ -1622,6 +1622,8 @@ public fun <T : Comparable<T>> Iterable<T>.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 <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? {
|
||||
val iterator = iterator()
|
||||
@@ -1707,6 +1709,8 @@ public fun <T : Comparable<T>> Iterable<T>.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 <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R): T? {
|
||||
val iterator = iterator()
|
||||
|
||||
@@ -147,6 +147,8 @@ public inline fun <K, V> Map<out K, V>.forEach(action: (Map.Entry<K, V>) -> 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 <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
||||
@@ -163,6 +165,8 @@ public inline fun <K, V> Map<out K, V>.maxWith(comparator: Comparator<in Map.Ent
|
||||
|
||||
/**
|
||||
* Returns the first entry yielding the smallest value of the given function or `null` if there are no entries.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.minBy
|
||||
*/
|
||||
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
||||
return entries.minBy(selector)
|
||||
|
||||
@@ -1141,6 +1141,8 @@ public fun <T : Comparable<T>> Sequence<T>.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 <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R): T? {
|
||||
val iterator = iterator()
|
||||
@@ -1236,6 +1238,8 @@ public fun <T : Comparable<T>> Sequence<T>.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 <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R): T? {
|
||||
val iterator = iterator()
|
||||
|
||||
@@ -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 <R : Comparable<R>> 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 <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
@@ -497,6 +497,28 @@ class Collections {
|
||||
val emptyList = emptyList<Int>()
|
||||
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<Pair<String, Int>>()
|
||||
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<String>()
|
||||
val emptyMin = emptyList.minBy { it.length }
|
||||
assertPrints(emptyMin, "null")
|
||||
}
|
||||
}
|
||||
|
||||
class Elements {
|
||||
|
||||
@@ -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<R>")
|
||||
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<R>")
|
||||
returns("T?")
|
||||
body {
|
||||
|
||||
Reference in New Issue
Block a user