Introduce minByOrNull and maxByOrNull extension functions #KT-38854
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -412,63 +412,63 @@ class ArraysTest {
|
||||
expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it*it }) })
|
||||
}
|
||||
|
||||
@Test fun minBy() {
|
||||
expect(null, { arrayOf<Int>().minBy { it } })
|
||||
expect(1, { arrayOf(1).minBy { it } })
|
||||
expect(3, { arrayOf(2, 3).minBy { -it } })
|
||||
expect('a', { arrayOf('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { arrayOf("b", "abc").minBy { it.length } })
|
||||
@Test fun minByOrNull() {
|
||||
expect(null, { arrayOf<Int>().minByOrNull { it } })
|
||||
expect(1, { arrayOf(1).minByOrNull { it } })
|
||||
expect(3, { arrayOf(2, 3).minByOrNull { -it } })
|
||||
expect('a', { arrayOf('a', 'b').minByOrNull { "x$it" } })
|
||||
expect("b", { arrayOf("b", "abc").minByOrNull { it.length } })
|
||||
}
|
||||
|
||||
@Test fun minByInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().minBy { it } })
|
||||
expect(1, { intArrayOf(1).minBy { it } })
|
||||
expect(3, { intArrayOf(2, 3).minBy { -it } })
|
||||
expect(2000000000000, { longArrayOf(3000000000000, 2000000000000).minBy { it + 1 } })
|
||||
expect(1, { byteArrayOf(1, 3, 2).minBy { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).minBy { "a" } })
|
||||
expect(2.0F, { floatArrayOf(3.0F, 2.0F).minBy { it.toString() } })
|
||||
expect(2.0, { doubleArrayOf(2.0, 3.0).minBy { it * it } })
|
||||
@Test fun minByOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().minByOrNull { it } })
|
||||
expect(1, { intArrayOf(1).minByOrNull { it } })
|
||||
expect(3, { intArrayOf(2, 3).minByOrNull { -it } })
|
||||
expect(2000000000000, { longArrayOf(3000000000000, 2000000000000).minByOrNull { it + 1 } })
|
||||
expect(1, { byteArrayOf(1, 3, 2).minByOrNull { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).minByOrNull { "a" } })
|
||||
expect(2.0F, { floatArrayOf(3.0F, 2.0F).minByOrNull { it.toString() } })
|
||||
expect(2.0, { doubleArrayOf(2.0, 3.0).minByOrNull { it * it } })
|
||||
}
|
||||
|
||||
@Test fun maxBy() {
|
||||
expect(null, { arrayOf<Int>().maxBy { it } })
|
||||
expect(1, { arrayOf(1).maxBy { it } })
|
||||
expect(2, { arrayOf(2, 3).maxBy { -it } })
|
||||
expect('b', { arrayOf('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { arrayOf("b", "abc").maxBy { it.length } })
|
||||
@Test fun maxByOrNull() {
|
||||
expect(null, { arrayOf<Int>().maxByOrNull { it } })
|
||||
expect(1, { arrayOf(1).maxByOrNull { it } })
|
||||
expect(2, { arrayOf(2, 3).maxByOrNull { -it } })
|
||||
expect('b', { arrayOf('a', 'b').maxByOrNull { "x$it" } })
|
||||
expect("abc", { arrayOf("b", "abc").maxByOrNull { it.length } })
|
||||
}
|
||||
|
||||
@Test fun maxByInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().maxBy { it } })
|
||||
expect(1, { intArrayOf(1).maxBy { it } })
|
||||
expect(2, { intArrayOf(2, 3).maxBy { -it } })
|
||||
expect(3000000000000, { longArrayOf(3000000000000, 2000000000000).maxBy { it + 1 } })
|
||||
expect(3, { byteArrayOf(1, 3, 2).maxBy { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).maxBy { "a" } })
|
||||
expect(3.0F, { floatArrayOf(3.0F, 2.0F).maxBy { it.toString() } })
|
||||
expect(3.0, { doubleArrayOf(2.0, 3.0).maxBy { it * it } })
|
||||
@Test fun maxByOrNullInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().maxByOrNull { it } })
|
||||
expect(1, { intArrayOf(1).maxByOrNull { it } })
|
||||
expect(2, { intArrayOf(2, 3).maxByOrNull { -it } })
|
||||
expect(3000000000000, { longArrayOf(3000000000000, 2000000000000).maxByOrNull { it + 1 } })
|
||||
expect(3, { byteArrayOf(1, 3, 2).maxByOrNull { it * it } })
|
||||
expect(3, { shortArrayOf(3, 2).maxByOrNull { "a" } })
|
||||
expect(3.0F, { floatArrayOf(3.0F, 2.0F).maxByOrNull { it.toString() } })
|
||||
expect(3.0, { doubleArrayOf(2.0, 3.0).maxByOrNull { it * it } })
|
||||
}
|
||||
|
||||
@Test fun minIndex() {
|
||||
val a = intArrayOf(1, 7, 9, -42, 54, 93)
|
||||
expect(3, { a.indices.minBy { a[it] } })
|
||||
expect(3, { a.indices.minByOrNull { a[it] } })
|
||||
}
|
||||
|
||||
@Test fun maxIndex() {
|
||||
val a = intArrayOf(1, 7, 9, 239, 54, 93)
|
||||
expect(3, { a.indices.maxBy { a[it] } })
|
||||
expect(3, { a.indices.maxByOrNull { a[it] } })
|
||||
}
|
||||
|
||||
@Test fun minByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { arrayOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
expect(1, { arrayOf(5, 4, 3, 2, 1).minByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
@Test fun maxByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { arrayOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
expect(5, { arrayOf(5, 4, 3, 2, 1).maxByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
|
||||
@@ -865,41 +865,41 @@ class CollectionTest {
|
||||
expect("B", { listOf("a", "B").asSequence().maxWith(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
}
|
||||
|
||||
@Test fun minBy() {
|
||||
expect(null, { listOf<Int>().minBy { it } })
|
||||
expect(1, { listOf(1).minBy { it } })
|
||||
expect(3, { listOf(2, 3).minBy { -it } })
|
||||
expect('a', { listOf('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { listOf("b", "abc").minBy { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().minBy { it } })
|
||||
expect(3, { listOf(2, 3).asSequence().minBy { -it } })
|
||||
@Test fun minByOrNull() {
|
||||
expect(null, { listOf<Int>().minByOrNull { it } })
|
||||
expect(1, { listOf(1).minByOrNull { it } })
|
||||
expect(3, { listOf(2, 3).minByOrNull { -it } })
|
||||
expect('a', { listOf('a', 'b').minByOrNull { "x$it" } })
|
||||
expect("b", { listOf("b", "abc").minByOrNull { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().minByOrNull { it } })
|
||||
expect(3, { listOf(2, 3).asSequence().minByOrNull { -it } })
|
||||
}
|
||||
|
||||
@Test fun maxBy() {
|
||||
expect(null, { listOf<Int>().maxBy { it } })
|
||||
expect(1, { listOf(1).maxBy { it } })
|
||||
expect(2, { listOf(2, 3).maxBy { -it } })
|
||||
expect('b', { listOf('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { listOf("b", "abc").maxBy { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().maxBy { it } })
|
||||
expect(2, { listOf(2, 3).asSequence().maxBy { -it } })
|
||||
@Test fun maxByOrNull() {
|
||||
expect(null, { listOf<Int>().maxByOrNull { it } })
|
||||
expect(1, { listOf(1).maxByOrNull { it } })
|
||||
expect(2, { listOf(2, 3).maxByOrNull { -it } })
|
||||
expect('b', { listOf('a', 'b').maxByOrNull { "x$it" } })
|
||||
expect("abc", { listOf("b", "abc").maxByOrNull { it.length } })
|
||||
expect(null, { listOf<Int>().asSequence().maxByOrNull { it } })
|
||||
expect(2, { listOf(2, 3).asSequence().maxByOrNull { -it } })
|
||||
}
|
||||
|
||||
@Test fun minByEvaluateOnce() {
|
||||
@Test fun minByOrNullEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).minByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).asSequence().minBy { c++; it * it } })
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).asSequence().minByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
@Test fun maxByEvaluateOnce() {
|
||||
@Test fun maxByOrNullEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).maxByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).asSequence().maxBy { c++; it * it } })
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).asSequence().maxByOrNull { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
|
||||
@@ -396,13 +396,13 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
@Test
|
||||
fun maxOrNull() {
|
||||
expect("foo") { data.maxOrNull() }
|
||||
expect("bar") { data.maxBy { it.last() } }
|
||||
expect("bar") { data.maxByOrNull { it.last() } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minOrNull() {
|
||||
expect("bar") { data.minOrNull() }
|
||||
expect("foo") { data.minBy { it.last() } }
|
||||
expect("foo") { data.minByOrNull { it.last() } }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -483,35 +483,35 @@ class UnsignedArraysTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minBy() {
|
||||
expect(null) { arrayOf<UByte>().minBy { it * it } }
|
||||
expect(1u) { arrayOf<UShort>(1).minBy { it * it } }
|
||||
expect(2u) { arrayOf<UInt>(2, 3).minBy { it * it } }
|
||||
expect(3uL) { arrayOf<ULong>(3, 2).minBy { it - 3 } }
|
||||
fun minByOrNull() {
|
||||
expect(null) { arrayOf<UByte>().minByOrNull { it * it } }
|
||||
expect(1u) { arrayOf<UShort>(1).minByOrNull { it * it } }
|
||||
expect(2u) { arrayOf<UInt>(2, 3).minByOrNull { it * it } }
|
||||
expect(3uL) { arrayOf<ULong>(3, 2).minByOrNull { it - 3 } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun minByInUnsignedArrays() {
|
||||
expect(null) { ubyteArrayOf().minBy { it * it } }
|
||||
expect(1u) { ushortArrayOf(1).minBy { it * it } }
|
||||
expect(2u) { uintArrayOf(2, 3).minBy { it * it } }
|
||||
expect(3uL) { ulongArrayOf(3, 2).minBy { it - 3 } }
|
||||
fun minByOrNullInUnsignedArrays() {
|
||||
expect(null) { ubyteArrayOf().minByOrNull { it * it } }
|
||||
expect(1u) { ushortArrayOf(1).minByOrNull { it * it } }
|
||||
expect(2u) { uintArrayOf(2, 3).minByOrNull { it * it } }
|
||||
expect(3uL) { ulongArrayOf(3, 2).minByOrNull { it - 3 } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxBy() {
|
||||
expect(null) { arrayOf<UByte>().maxBy { it + 1 } }
|
||||
expect(1u) { arrayOf<UShort>(1).maxBy { it + 1 } }
|
||||
expect(2u) { arrayOf<UInt>(2, 3).maxBy { it - 3 } }
|
||||
expect(3uL) { arrayOf<ULong>(3, 2).maxBy { it + 1 } }
|
||||
fun maxByOrNull() {
|
||||
expect(null) { arrayOf<UByte>().maxByOrNull { it + 1 } }
|
||||
expect(1u) { arrayOf<UShort>(1).maxByOrNull { it + 1 } }
|
||||
expect(2u) { arrayOf<UInt>(2, 3).maxByOrNull { it - 3 } }
|
||||
expect(3uL) { arrayOf<ULong>(3, 2).maxByOrNull { it + 1 } }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun maxByInUnsignedArrays() {
|
||||
expect(null) { ubyteArrayOf().maxBy { it + 1 } }
|
||||
expect(1u) { ushortArrayOf(1).maxBy { it + 1 } }
|
||||
expect(2u) { uintArrayOf(2, 3).maxBy { it - 3 } }
|
||||
expect(3uL) { ulongArrayOf(3, 2).maxBy { it + 1 } }
|
||||
fun maxByOrNullInUnsignedArrays() {
|
||||
expect(null) { ubyteArrayOf().maxByOrNull { it + 1 } }
|
||||
expect(1u) { ushortArrayOf(1).maxByOrNull { it + 1 } }
|
||||
expect(2u) { uintArrayOf(2, 3).maxByOrNull { it - 3 } }
|
||||
expect(3uL) { ulongArrayOf(3, 2).maxByOrNull { it + 1 } }
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -996,7 +996,7 @@ class UnsignedArraysTest {
|
||||
),
|
||||
ushortArrayOf(1, 2, 3).withIndex()
|
||||
)
|
||||
assertEquals(IndexedValue(1, 2.toUInt()), uintArrayOf(1, 2, 3).withIndex().minBy { it.value % 2 })
|
||||
assertEquals(IndexedValue(1, 2.toUInt()), uintArrayOf(1, 2, 3).withIndex().minByOrNull { it.value % 2 })
|
||||
assertIterableContentEquals(listOf(0, 1, 2), ulongArrayOf(1, 2, 3).withIndex().map { it.index })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user