Reintroduce min/max operations for non-empty collections KT-50146

This commit is contained in:
Ilya Gorbunov
2022-03-10 20:09:24 +03:00
committed by Space
parent 4709e2c274
commit 60fbb0d171
20 changed files with 3796 additions and 1250 deletions
+46 -16
View File
@@ -211,11 +211,19 @@ public inline fun <K, V> Map<out K, V>.forEach(action: (Map.Entry<K, V>) -> Unit
for (element in this) action(element)
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("this.maxByOrNull(selector)"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
/**
* Returns the first entry yielding the largest value of the given function.
*
* @throws NoSuchElementException if the map is empty.
*
* @sample samples.collections.Collections.Aggregates.maxBy
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("maxByOrThrow")
@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>? {
return maxByOrNull(selector)
@Suppress("CONFLICTING_OVERLOADS")
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V> {
return entries.maxBy(selector)
}
/**
@@ -341,11 +349,17 @@ public inline fun <K, V, R> Map<out K, V>.maxOfWithOrNull(comparator: Comparator
return entries.maxOfWithOrNull(comparator, selector)
}
@Deprecated("Use maxWithOrNull instead.", ReplaceWith("this.maxWithOrNull(comparator)"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
/**
* Returns the first entry having the largest value according to the provided [comparator].
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("maxWithOrThrow")
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.maxWith(comparator: Comparator<in Map.Entry<K, V>>): Map.Entry<K, V>? {
return maxWithOrNull(comparator)
@Suppress("CONFLICTING_OVERLOADS")
public inline fun <K, V> Map<out K, V>.maxWith(comparator: Comparator<in Map.Entry<K, V>>): Map.Entry<K, V> {
return entries.maxWith(comparator)
}
/**
@@ -357,10 +371,19 @@ public inline fun <K, V> Map<out K, V>.maxWithOrNull(comparator: Comparator<in M
return entries.maxWithOrNull(comparator)
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("this.minByOrNull(selector)"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
return minByOrNull(selector)
/**
* Returns the first entry yielding the smallest value of the given function.
*
* @throws NoSuchElementException if the map is empty.
*
* @sample samples.collections.Collections.Aggregates.minBy
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("minByOrThrow")
@kotlin.internal.InlineOnly
@Suppress("CONFLICTING_OVERLOADS")
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)
}
/**
@@ -486,10 +509,17 @@ public inline fun <K, V, R> Map<out K, V>.minOfWithOrNull(comparator: Comparator
return entries.minOfWithOrNull(comparator, selector)
}
@Deprecated("Use minWithOrNull instead.", ReplaceWith("this.minWithOrNull(comparator)"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
public fun <K, V> Map<out K, V>.minWith(comparator: Comparator<in Map.Entry<K, V>>): Map.Entry<K, V>? {
return minWithOrNull(comparator)
/**
* Returns the first entry having the smallest value according to the provided [comparator].
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.7")
@kotlin.jvm.JvmName("minWithOrThrow")
@kotlin.internal.InlineOnly
@Suppress("CONFLICTING_OVERLOADS")
public inline fun <K, V> Map<out K, V>.minWith(comparator: Comparator<in Map.Entry<K, V>>): Map.Entry<K, V> {
return entries.minWith(comparator)
}
/**