Introduce minByOrNull and maxByOrNull extension functions #KT-38854

This commit is contained in:
Abduqodiri Qurbonzoda
2020-06-15 23:27:28 +03:00
parent 846a7823ad
commit 194791a168
23 changed files with 801 additions and 594 deletions
+386 -278
View File
@@ -13109,166 +13109,58 @@ public fun CharArray.max(): Char? {
return maxOrNull()
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Array<out T>.maxBy(selector: (T) -> R): T? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> ByteArray.maxBy(selector: (Byte) -> R): Byte? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> ShortArray.maxBy(selector: (Short) -> R): Short? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> IntArray.maxBy(selector: (Int) -> R): Int? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> LongArray.maxBy(selector: (Long) -> R): Long? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> FloatArray.maxBy(selector: (Float) -> R): Float? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> DoubleArray.maxBy(selector: (Double) -> R): Double? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? {
return maxByOrNull(selector)
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> CharArray.maxBy(selector: (Char) -> R): Char? {
return maxByOrNull(selector)
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Array<out T>.maxByOrNull(selector: (T) -> R): T? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
@@ -13290,7 +13182,169 @@ public inline fun <R : Comparable<R>> BooleanArray.maxBy(selector: (Boolean) ->
*
* @sample samples.collections.Collections.Aggregates.maxBy
*/
public inline fun <R : Comparable<R>> CharArray.maxBy(selector: (Char) -> R): Char? {
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> ByteArray.maxByOrNull(selector: (Byte) -> R): Byte? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> ShortArray.maxByOrNull(selector: (Short) -> R): Short? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> IntArray.maxByOrNull(selector: (Int) -> R): Int? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> LongArray.maxByOrNull(selector: (Long) -> R): Long? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> FloatArray.maxByOrNull(selector: (Float) -> R): Float? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> DoubleArray.maxByOrNull(selector: (Double) -> R): Double? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> BooleanArray.maxByOrNull(selector: (Boolean) -> R): Boolean? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> CharArray.maxByOrNull(selector: (Char) -> R): Char? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
@@ -15136,166 +15190,58 @@ public fun CharArray.min(): Char? {
return minOrNull()
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Array<out T>.minBy(selector: (T) -> R): T? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> ByteArray.minBy(selector: (Byte) -> R): Byte? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> ShortArray.minBy(selector: (Short) -> R): Short? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> IntArray.minBy(selector: (Int) -> R): Int? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> LongArray.minBy(selector: (Long) -> R): Long? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> FloatArray.minBy(selector: (Float) -> R): Float? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> DoubleArray.minBy(selector: (Double) -> R): Double? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> BooleanArray.minBy(selector: (Boolean) -> R): Boolean? {
return minByOrNull(selector)
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> CharArray.minBy(selector: (Char) -> R): Char? {
return minByOrNull(selector)
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Array<out T>.minByOrNull(selector: (T) -> R): T? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
@@ -15317,7 +15263,169 @@ public inline fun <R : Comparable<R>> BooleanArray.minBy(selector: (Boolean) ->
*
* @sample samples.collections.Collections.Aggregates.minBy
*/
public inline fun <R : Comparable<R>> CharArray.minBy(selector: (Char) -> R): Char? {
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> ByteArray.minByOrNull(selector: (Byte) -> R): Byte? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> ShortArray.minByOrNull(selector: (Short) -> R): Short? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> IntArray.minByOrNull(selector: (Int) -> R): Int? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> LongArray.minByOrNull(selector: (Long) -> R): Long? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> FloatArray.minByOrNull(selector: (Float) -> R): Float? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> DoubleArray.minByOrNull(selector: (Double) -> R): Double? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> BooleanArray.minByOrNull(selector: (Boolean) -> R): Boolean? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> CharArray.minByOrNull(selector: (Char) -> R): Char? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
@@ -1736,12 +1736,18 @@ public fun <T : Comparable<T>> Iterable<T>.max(): T? {
return maxOrNull()
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? {
return maxByOrNull(selector)
}
/**
* 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? {
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Iterable<T>.maxByOrNull(selector: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxElem = iterator.next()
@@ -2014,12 +2020,18 @@ public fun <T : Comparable<T>> Iterable<T>.min(): T? {
return minOrNull()
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R): T? {
return minByOrNull(selector)
}
/**
* 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? {
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Iterable<T>.minByOrNull(selector: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minElem = iterator.next()
+18 -4
View File
@@ -179,14 +179,21 @@ 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("maxByOrNull(selector)"))
@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)
}
/**
* 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
*/
@SinceKotlin("1.4")
@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 entries.maxBy(selector)
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxByOrNull(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
return entries.maxByOrNull(selector)
}
/**
@@ -309,13 +316,20 @@ public inline fun <K, V> Map<out K, V>.maxWith(comparator: Comparator<in Map.Ent
return entries.maxWith(comparator)
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
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 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)
@SinceKotlin("1.4")
@kotlin.internal.InlineOnly
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minByOrNull(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
return entries.minByOrNull(selector)
}
/**
@@ -1183,6 +1183,11 @@ public fun <T : Comparable<T>> Sequence<T>.max(): T? {
return maxOrNull()
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R): T? {
return maxByOrNull(selector)
}
/**
* Returns the first element yielding the largest value of the given function or `null` if there are no elements.
*
@@ -1190,7 +1195,8 @@ public fun <T : Comparable<T>> Sequence<T>.max(): T? {
*
* @sample samples.collections.Collections.Aggregates.maxBy
*/
public inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R): T? {
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Sequence<T>.maxByOrNull(selector: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxElem = iterator.next()
@@ -1487,6 +1493,11 @@ public fun <T : Comparable<T>> Sequence<T>.min(): T? {
return minOrNull()
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R): T? {
return minByOrNull(selector)
}
/**
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements.
*
@@ -1494,7 +1505,8 @@ public fun <T : Comparable<T>> Sequence<T>.min(): T? {
*
* @sample samples.collections.Collections.Aggregates.minBy
*/
public inline fun <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R): T? {
@SinceKotlin("1.4")
public inline fun <T, R : Comparable<R>> Sequence<T>.minByOrNull(selector: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minElem = iterator.next()
@@ -1093,12 +1093,18 @@ public fun CharSequence.max(): Char? {
return maxOrNull()
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R): Char? {
return maxByOrNull(selector)
}
/**
* 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? {
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> CharSequence.maxByOrNull(selector: (Char) -> R): Char? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
@@ -1315,12 +1321,18 @@ public fun CharSequence.min(): Char? {
return minOrNull()
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char? {
return minByOrNull(selector)
}
/**
* 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? {
@SinceKotlin("1.4")
public inline fun <R : Comparable<R>> CharSequence.minByOrNull(selector: (Char) -> R): Char? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
+154 -90
View File
@@ -5730,65 +5730,47 @@ public fun UShortArray.max(): UShort? {
return maxOrNull()
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UIntArray.maxBy(selector: (UInt) -> R): UInt? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> ULongArray.maxBy(selector: (ULong) -> R): ULong? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
return maxByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UByteArray.maxBy(selector: (UByte) -> R): UByte? {
return maxByOrNull(selector)
}
@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.maxBy(selector: (UShort) -> R): UShort? {
return maxByOrNull(selector)
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UIntArray.maxByOrNull(selector: (UInt) -> R): UInt? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
@@ -5810,10 +5792,60 @@ public inline fun <R : Comparable<R>> UByteArray.maxBy(selector: (UByte) -> R):
*
* @sample samples.collections.Collections.Aggregates.maxBy
*/
@SinceKotlin("1.3")
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.maxBy(selector: (UShort) -> R): UShort? {
public inline fun <R : Comparable<R>> ULongArray.maxByOrNull(selector: (ULong) -> R): ULong? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UByteArray.maxByOrNull(selector: (UByte) -> R): UByte? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxElem
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.maxByOrNull(selector: (UShort) -> R): UShort? {
if (isEmpty()) return null
var maxElem = this[0]
val lastIndex = this.lastIndex
@@ -6682,65 +6714,47 @@ public fun UShortArray.min(): UShort? {
return minOrNull()
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UIntArray.minBy(selector: (UInt) -> R): UInt? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> ULongArray.minBy(selector: (ULong) -> R): ULong? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
return minByOrNull(selector)
}
/**
* 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
*/
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UByteArray.minBy(selector: (UByte) -> R): UByte? {
return minByOrNull(selector)
}
@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)"))
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.minBy(selector: (UShort) -> R): UShort? {
return minByOrNull(selector)
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UIntArray.minByOrNull(selector: (UInt) -> R): UInt? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
@@ -6762,10 +6776,60 @@ public inline fun <R : Comparable<R>> UByteArray.minBy(selector: (UByte) -> R):
*
* @sample samples.collections.Collections.Aggregates.minBy
*/
@SinceKotlin("1.3")
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.minBy(selector: (UShort) -> R): UShort? {
public inline fun <R : Comparable<R>> ULongArray.minByOrNull(selector: (ULong) -> R): ULong? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UByteArray.minByOrNull(selector: (UByte) -> R): UByte? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex
if (lastIndex == 0) return minElem
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* 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
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> UShortArray.minByOrNull(selector: (UShort) -> R): UShort? {
if (isEmpty()) return null
var minElem = this[0]
val lastIndex = this.lastIndex