Skip to content

Commit

Permalink
Improve docs of max() and min() functions on collections #KT-36863
Browse files Browse the repository at this point in the history
Also for:
* maxOf() and minOf()
* maxOfWith() and minOfWith()

Specify which element is returned from the functions if multiple
elements are equally maximal or minimal. Add samples.
  • Loading branch information
qurbonzoda authored and qodana-bot committed Dec 5, 2024
1 parent 1995883 commit 9cb1738
Show file tree
Hide file tree
Showing 8 changed files with 1,433 additions and 217 deletions.
736 changes: 628 additions & 108 deletions libraries/stdlib/common/src/generated/_Arrays.kt

Large diffs are not rendered by default.

124 changes: 102 additions & 22 deletions libraries/stdlib/common/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1876,9 +1876,11 @@ public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit
/**
* Returns the largest element.
*
* If any of elements is `NaN` returns `NaN`.
* If any of elements is `NaN`, this function returns `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("maxOrThrow")
Expand All @@ -1897,9 +1899,11 @@ public fun Iterable<Double>.max(): Double {
/**
* Returns the largest element.
*
* If any of elements is `NaN` returns `NaN`.
* If any of elements is `NaN`, this function returns `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("maxOrThrow")
Expand All @@ -1918,7 +1922,11 @@ public fun Iterable<Float>.max(): Float {
/**
* Returns the largest element.
*
* If there are multiple equal maximal elements, this function returns the first of those elements.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinGeneric
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("maxOrThrow")
Expand Down Expand Up @@ -1991,6 +1999,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxByOrNull(selector: (T) -
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2014,6 +2024,8 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2034,7 +2046,11 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Float): Float {
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection.
*
* If multiple elements produce the maximal value, this function returns the first of those values.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2055,9 +2071,11 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):

/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2076,9 +2094,11 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double?

/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2097,7 +2117,11 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Float): Float? {

/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If multiple elements produce the maximal value, this function returns the first of those values.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2120,7 +2144,11 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOfOrNull(selector: (T) -
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection.
*
* If multiple elements produce the maximal value, this function returns the first of those values.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfWithMinOfWithGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2141,7 +2169,11 @@ public inline fun <T, R> Iterable<T>.maxOfWith(comparator: Comparator<in R>, sel

/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
* among all values produced by [selector] function applied to each element in the collection or `null` if the collection is empty.
*
* If multiple elements produce the maximal value, this function returns the first of those values.
*
* @sample samples.collections.Collections.Aggregates.maxOfWithMinOfWithGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2161,9 +2193,11 @@ public inline fun <T, R> Iterable<T>.maxOfWithOrNull(comparator: Comparator<in R
}

/**
* Returns the largest element or `null` if there are no elements.
* Returns the largest element or `null` if the collection is empty.
*
* If any of elements is `NaN`, this function returns `NaN`.
*
* If any of elements is `NaN` returns `NaN`.
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.4")
public fun Iterable<Double>.maxOrNull(): Double? {
Expand All @@ -2178,9 +2212,11 @@ public fun Iterable<Double>.maxOrNull(): Double? {
}

/**
* Returns the largest element or `null` if there are no elements.
* Returns the largest element or `null` if the collection is empty.
*
* If any of elements is `NaN`, this function returns `NaN`.
*
* If any of elements is `NaN` returns `NaN`.
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.4")
public fun Iterable<Float>.maxOrNull(): Float? {
Expand All @@ -2195,7 +2231,11 @@ public fun Iterable<Float>.maxOrNull(): Float? {
}

/**
* Returns the largest element or `null` if there are no elements.
* Returns the largest element or `null` if the collection is empty.
*
* If there are multiple equal maximal elements, this function returns the first of those elements.
*
* @sample samples.collections.Collections.Aggregates.maxMinGeneric
*/
@SinceKotlin("1.4")
public fun <T : Comparable<T>> Iterable<T>.maxOrNull(): T? {
Expand Down Expand Up @@ -2246,9 +2286,11 @@ public fun <T> Iterable<T>.maxWithOrNull(comparator: Comparator<in T>): T? {
/**
* Returns the smallest element.
*
* If any of elements is `NaN` returns `NaN`.
* If any of elements is `NaN`, this function returns `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("minOrThrow")
Expand All @@ -2267,9 +2309,11 @@ public fun Iterable<Double>.min(): Double {
/**
* Returns the smallest element.
*
* If any of elements is `NaN` returns `NaN`.
* If any of elements is `NaN`, this function returns `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("minOrThrow")
Expand All @@ -2288,7 +2332,11 @@ public fun Iterable<Float>.min(): Float {
/**
* Returns the smallest element.
*
* If there are multiple equal minimal elements, this function returns the first of those elements.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxMinGeneric
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("minOrThrow")
Expand Down Expand Up @@ -2361,6 +2409,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minByOrNull(selector: (T) -
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2384,6 +2434,8 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2404,7 +2456,11 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Float): Float {
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection.
*
* If multiple elements produce the minimal value, this function returns the first of those values.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2425,9 +2481,11 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):

/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2446,9 +2504,11 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double?

/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfFloatingResult
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2467,7 +2527,11 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Float): Float? {

/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
* applied to each element in the collection or `null` if the collection is empty.
*
* If multiple elements produce the minimal value, this function returns the first of those values.
*
* @sample samples.collections.Collections.Aggregates.maxOfMinOfGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2490,7 +2554,11 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOfOrNull(selector: (T) -
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection.
*
* If multiple elements produce the minimal value, this function returns the first of those values.
*
* @throws NoSuchElementException if the collection is empty.
*
* @sample samples.collections.Collections.Aggregates.maxOfWithMinOfWithGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2511,7 +2579,11 @@ public inline fun <T, R> Iterable<T>.minOfWith(comparator: Comparator<in R>, sel

/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
* among all values produced by [selector] function applied to each element in the collection or `null` if the collection is empty.
*
* If multiple elements produce the minimal value, this function returns the first of those values.
*
* @sample samples.collections.Collections.Aggregates.maxOfWithMinOfWithGeneric
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
Expand All @@ -2531,9 +2603,11 @@ public inline fun <T, R> Iterable<T>.minOfWithOrNull(comparator: Comparator<in R
}

/**
* Returns the smallest element or `null` if there are no elements.
* Returns the smallest element or `null` if the collection is empty.
*
* If any of elements is `NaN`, this function returns `NaN`.
*
* If any of elements is `NaN` returns `NaN`.
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.4")
public fun Iterable<Double>.minOrNull(): Double? {
Expand All @@ -2548,9 +2622,11 @@ public fun Iterable<Double>.minOrNull(): Double? {
}

/**
* Returns the smallest element or `null` if there are no elements.
* Returns the smallest element or `null` if the collection is empty.
*
* If any of elements is `NaN`, this function returns `NaN`.
*
* If any of elements is `NaN` returns `NaN`.
* @sample samples.collections.Collections.Aggregates.maxMinFloating
*/
@SinceKotlin("1.4")
public fun Iterable<Float>.minOrNull(): Float? {
Expand All @@ -2565,7 +2641,11 @@ public fun Iterable<Float>.minOrNull(): Float? {
}

/**
* Returns the smallest element or `null` if there are no elements.
* Returns the smallest element or `null` if the collection is empty.
*
* If there are multiple equal minimal elements, this function returns the first of those elements.
*
* @sample samples.collections.Collections.Aggregates.maxMinGeneric
*/
@SinceKotlin("1.4")
public fun <T : Comparable<T>> Iterable<T>.minOrNull(): T? {
Expand Down
Loading

0 comments on commit 9cb1738

Please sign in to comment.