Introduce minByOrNull and maxByOrNull extension functions #KT-38854
This commit is contained in:
Generated
+3
-3
@@ -1284,9 +1284,9 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+4
-4
@@ -5,16 +5,16 @@ fun box(): String {
|
||||
val intList = listOf(1, 2, 3)
|
||||
val longList = listOf(1L, 2L, 3L)
|
||||
|
||||
val intListMin = intList.minBy { it }
|
||||
val intListMin = intList.minByOrNull { it }
|
||||
if (intListMin != 1) return "Fail intListMin=$intListMin"
|
||||
|
||||
val intListMax = intList.maxBy { it }
|
||||
val intListMax = intList.maxByOrNull { it }
|
||||
if (intListMax != 3) return "Fail intListMax=$intListMax"
|
||||
|
||||
val longListMin = longList.minBy { it }
|
||||
val longListMin = longList.minByOrNull { it }
|
||||
if (longListMin != 1L) return "Fail longListMin=$longListMin"
|
||||
|
||||
val longListMax = longList.maxBy { it }
|
||||
val longListMax = longList.maxByOrNull { it }
|
||||
if (longListMax != 3L) return "Fail longListMax=$longListMax"
|
||||
|
||||
return "OK"
|
||||
+4
-4
@@ -6,16 +6,16 @@ val longList = listOf(1L, 2L, 3L)
|
||||
// FILE: box.kt
|
||||
|
||||
fun box(): String {
|
||||
val intListMin = intList.minBy { it } ?: -1
|
||||
val intListMin = intList.minByOrNull { it } ?: -1
|
||||
if (intListMin != 1) return "Fail intListMin=$intListMin"
|
||||
|
||||
val intListMax = intList.maxBy { it } ?: -1
|
||||
val intListMax = intList.maxByOrNull { it } ?: -1
|
||||
if (intListMax != 3) return "Fail intListMax=$intListMax"
|
||||
|
||||
val longListMin = longList.minBy { it } ?: -1
|
||||
val longListMin = longList.minByOrNull { it } ?: -1
|
||||
if (longListMin != 1L) return "Fail longListMin=$longListMin"
|
||||
|
||||
val longListMax = longList.maxBy { it } ?: -1
|
||||
val longListMax = longList.maxByOrNull { it } ?: -1
|
||||
if (longListMax != 3L) return "Fail longListMax=$longListMax"
|
||||
|
||||
return "OK"
|
||||
+3
-3
@@ -1304,9 +1304,9 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+3
-3
@@ -600,9 +600,9 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/kt7224.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+3
-3
@@ -1304,9 +1304,9 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+3
-3
@@ -1284,9 +1284,9 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+3
-3
@@ -600,9 +600,9 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/kt7224.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
Generated
+3
-3
@@ -919,9 +919,9 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
Generated
+3
-3
@@ -919,9 +919,9 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
+3
-3
@@ -919,9 +919,9 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/kt6842.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("maxMinBy.kt")
|
||||
public void testMaxMinBy() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt");
|
||||
@TestMetadata("maxMinByOrNull.kt")
|
||||
public void testMaxMinByOrNull() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullCheck.kt")
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
+24
@@ -1393,6 +1393,15 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun maxBy ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxBy ([SLkotlin/jvm/functions/Function1;)Ljava/lang/Short;
|
||||
public static final fun maxBy ([ZLkotlin/jvm/functions/Function1;)Ljava/lang/Boolean;
|
||||
public static final fun maxByOrNull ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte;
|
||||
public static final fun maxByOrNull ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun maxByOrNull ([DLkotlin/jvm/functions/Function1;)Ljava/lang/Double;
|
||||
public static final fun maxByOrNull ([FLkotlin/jvm/functions/Function1;)Ljava/lang/Float;
|
||||
public static final fun maxByOrNull ([ILkotlin/jvm/functions/Function1;)Ljava/lang/Integer;
|
||||
public static final fun maxByOrNull ([JLkotlin/jvm/functions/Function1;)Ljava/lang/Long;
|
||||
public static final fun maxByOrNull ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxByOrNull ([SLkotlin/jvm/functions/Function1;)Ljava/lang/Short;
|
||||
public static final fun maxByOrNull ([ZLkotlin/jvm/functions/Function1;)Ljava/lang/Boolean;
|
||||
public static final fun maxOrNull ([B)Ljava/lang/Byte;
|
||||
public static final fun maxOrNull ([C)Ljava/lang/Character;
|
||||
public static final fun maxOrNull ([D)Ljava/lang/Double;
|
||||
@@ -1431,6 +1440,15 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun minBy ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minBy ([SLkotlin/jvm/functions/Function1;)Ljava/lang/Short;
|
||||
public static final fun minBy ([ZLkotlin/jvm/functions/Function1;)Ljava/lang/Boolean;
|
||||
public static final fun minByOrNull ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte;
|
||||
public static final fun minByOrNull ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun minByOrNull ([DLkotlin/jvm/functions/Function1;)Ljava/lang/Double;
|
||||
public static final fun minByOrNull ([FLkotlin/jvm/functions/Function1;)Ljava/lang/Float;
|
||||
public static final fun minByOrNull ([ILkotlin/jvm/functions/Function1;)Ljava/lang/Integer;
|
||||
public static final fun minByOrNull ([JLkotlin/jvm/functions/Function1;)Ljava/lang/Long;
|
||||
public static final fun minByOrNull ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minByOrNull ([SLkotlin/jvm/functions/Function1;)Ljava/lang/Short;
|
||||
public static final fun minByOrNull ([ZLkotlin/jvm/functions/Function1;)Ljava/lang/Boolean;
|
||||
public static final fun minOrNull ([B)Ljava/lang/Byte;
|
||||
public static final fun minOrNull ([C)Ljava/lang/Character;
|
||||
public static final fun minOrNull ([D)Ljava/lang/Double;
|
||||
@@ -2215,6 +2233,7 @@ public final class kotlin/collections/CollectionsKt {
|
||||
public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Double;
|
||||
public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Float;
|
||||
public static final fun maxBy (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxByOrNull (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxOrNull (Ljava/lang/Iterable;)Ljava/lang/Comparable;
|
||||
public static final fun maxOrNull (Ljava/lang/Iterable;)Ljava/lang/Double;
|
||||
public static final fun maxOrNull (Ljava/lang/Iterable;)Ljava/lang/Float;
|
||||
@@ -2223,6 +2242,7 @@ public final class kotlin/collections/CollectionsKt {
|
||||
public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Double;
|
||||
public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Float;
|
||||
public static final fun minBy (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minByOrNull (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minOrNull (Ljava/lang/Iterable;)Ljava/lang/Comparable;
|
||||
public static final fun minOrNull (Ljava/lang/Iterable;)Ljava/lang/Double;
|
||||
public static final fun minOrNull (Ljava/lang/Iterable;)Ljava/lang/Float;
|
||||
@@ -4872,6 +4892,7 @@ public final class kotlin/sequences/SequencesKt {
|
||||
public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Double;
|
||||
public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Float;
|
||||
public static final fun maxBy (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxByOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun maxOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable;
|
||||
public static final fun maxOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Double;
|
||||
public static final fun maxOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Float;
|
||||
@@ -4880,6 +4901,7 @@ public final class kotlin/sequences/SequencesKt {
|
||||
public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Double;
|
||||
public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Float;
|
||||
public static final fun minBy (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minByOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun minOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable;
|
||||
public static final fun minOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Double;
|
||||
public static final fun minOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Float;
|
||||
@@ -5281,10 +5303,12 @@ public final class kotlin/text/StringsKt {
|
||||
public static final fun mapTo (Ljava/lang/CharSequence;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection;
|
||||
public static final fun max (Ljava/lang/CharSequence;)Ljava/lang/Character;
|
||||
public static final fun maxBy (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun maxByOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun maxOrNull (Ljava/lang/CharSequence;)Ljava/lang/Character;
|
||||
public static final fun maxWith (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character;
|
||||
public static final fun min (Ljava/lang/CharSequence;)Ljava/lang/Character;
|
||||
public static final fun minBy (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun minByOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character;
|
||||
public static final fun minOrNull (Ljava/lang/CharSequence;)Ljava/lang/Character;
|
||||
public static final fun minWith (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character;
|
||||
public static final fun none (Ljava/lang/CharSequence;)Z
|
||||
|
||||
@@ -378,58 +378,74 @@ object Aggregates : TemplateGroupBase() {
|
||||
yield(def(op, nullable))
|
||||
}
|
||||
|
||||
val f_minBy = fn("minBy(selector: (T) -> R)") {
|
||||
includeDefault()
|
||||
include(Maps, CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
inline()
|
||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||
val f_minMaxBy = sequence {
|
||||
fun def(op: String, nullable: Boolean, orNull: String = "OrNull".ifOrEmpty(nullable)) =
|
||||
fn("$op$orNull(selector: (T) -> R)") {
|
||||
includeDefault()
|
||||
include(Maps, CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
inline()
|
||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||
specialFor(Maps) { if (op == "maxBy" || nullable) inlineOnly() }
|
||||
typeParam("R : Comparable<R>")
|
||||
returns("T?")
|
||||
|
||||
doc { "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element.pluralize()}." }
|
||||
sample("samples.collections.Collections.Aggregates.minBy")
|
||||
typeParam("R : Comparable<R>")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var minElem = iterator.next()
|
||||
if (!iterator.hasNext()) return minElem
|
||||
var minValue = selector(minElem)
|
||||
do {
|
||||
val e = iterator.next()
|
||||
val v = selector(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
if (!nullable) {
|
||||
deprecate(Deprecation("Use ${op}OrNull instead.", "${op}OrNull(selector)", DeprecationLevel.WARNING))
|
||||
body { "return ${op}OrNull(selector)" }
|
||||
return@builder
|
||||
}
|
||||
} while (iterator.hasNext())
|
||||
return minElem
|
||||
"""
|
||||
}
|
||||
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
"""
|
||||
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
|
||||
since("1.4")
|
||||
|
||||
doc { "Returns the first ${f.element} yielding the ${if (op == "maxBy") "largest" else "smallest"} value of the given function or `null` if there are no ${f.element.pluralize()}." }
|
||||
sample("samples.collections.Collections.Aggregates.$op")
|
||||
|
||||
val (elem, value, cmp) = if (op == "minBy") Triple("minElem", "minValue", ">") else Triple("maxElem", "maxValue", "<")
|
||||
body {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var $elem = iterator.next()
|
||||
if (!iterator.hasNext()) return $elem
|
||||
var $value = selector($elem)
|
||||
do {
|
||||
val e = iterator.next()
|
||||
val v = selector(e)
|
||||
if ($value $cmp v) {
|
||||
$elem = e
|
||||
$value = v
|
||||
}
|
||||
} while (iterator.hasNext())
|
||||
return $elem
|
||||
"""
|
||||
}
|
||||
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
"""
|
||||
if (isEmpty()) return null
|
||||
|
||||
var $elem = this[0]
|
||||
val lastIndex = this.lastIndex
|
||||
if (lastIndex == 0) return $elem
|
||||
var $value = selector($elem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = selector(e)
|
||||
if ($value $cmp v) {
|
||||
$elem = e
|
||||
$value = v
|
||||
}
|
||||
}
|
||||
return $elem
|
||||
"""
|
||||
}
|
||||
body(Maps) { "return entries.$op$orNull(selector)" }
|
||||
}
|
||||
return minElem
|
||||
"""
|
||||
}
|
||||
body(Maps) {
|
||||
"return entries.minBy(selector)"
|
||||
}
|
||||
|
||||
for (op in listOf("minBy", "maxBy"))
|
||||
for (nullable in listOf(false, true))
|
||||
yield(def(op, nullable))
|
||||
}
|
||||
|
||||
val f_minWith = fn("minWith(comparator: Comparator<in T>)") {
|
||||
@@ -465,61 +481,6 @@ object Aggregates : TemplateGroupBase() {
|
||||
body(Maps) { "return entries.minWith(comparator)" }
|
||||
}
|
||||
|
||||
val f_maxBy = fn("maxBy(selector: (T) -> R)") {
|
||||
includeDefault()
|
||||
include(Maps, CharSequences, ArraysOfUnsigned)
|
||||
} builder {
|
||||
inline()
|
||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
doc { "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element.pluralize()}." }
|
||||
sample("samples.collections.Collections.Aggregates.maxBy")
|
||||
typeParam("R : Comparable<R>")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var maxElem = iterator.next()
|
||||
if (!iterator.hasNext()) return maxElem
|
||||
var maxValue = selector(maxElem)
|
||||
do {
|
||||
val e = iterator.next()
|
||||
val v = selector(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
} while (iterator.hasNext())
|
||||
return maxElem
|
||||
"""
|
||||
}
|
||||
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
"""
|
||||
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
|
||||
"""
|
||||
}
|
||||
specialFor(Maps) {
|
||||
inlineOnly()
|
||||
body { "return entries.maxBy(selector)" }
|
||||
}
|
||||
}
|
||||
|
||||
val f_maxWith = fn("maxWith(comparator: Comparator<in T>)") {
|
||||
includeDefault()
|
||||
include(Maps, CharSequences, ArraysOfUnsigned)
|
||||
|
||||
Reference in New Issue
Block a user