diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 1c0070cb1d1..4811f3aa47f 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -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") diff --git a/compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt b/compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt similarity index 67% rename from compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt rename to compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt index 0d10c6e6ae5..217c5072d5a 100644 --- a/compiler/testData/codegen/box/boxingOptimization/maxMinBy.kt +++ b/compiler/testData/codegen/box/boxingOptimization/maxMinByOrNull.kt @@ -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" diff --git a/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt b/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinByOrNull.kt similarity index 83% rename from compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt rename to compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinByOrNull.kt index e58b164809c..99e795f0d0e 100644 --- a/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt +++ b/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinByOrNull.kt @@ -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" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b1b68c4e73f..4963fb8e3b0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 76bfcd3d84e..6afd474b698 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 79f34828c87..d1bceae8d90 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 1ac47eb03e9..d349c954911 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index f22f6d8a573..3cc34422512 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -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") diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 89fc1381aa5..733dccc556c 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -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") diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index f29e7f54eca..bac7b66be3a 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -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") diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 4caf0e9c4bf..d3a6f215777 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -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") diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index d08112477c7..19be6616b33 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.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 > Array.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 > 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 > 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 > 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 > 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 > 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 > 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 > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { + return maxByOrNull(selector) +} + +@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)")) +public inline fun > 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 > Array.maxByOrNull(selector: (T) -> R): T? { if (isEmpty()) return null var maxElem = this[0] val lastIndex = this.lastIndex @@ -13290,7 +13182,169 @@ public inline fun > BooleanArray.maxBy(selector: (Boolean) -> * * @sample samples.collections.Collections.Aggregates.maxBy */ -public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { +@SinceKotlin("1.4") +public inline fun > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > Array.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 > 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 > 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 > 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 > 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 > 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 > 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 > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { + return minByOrNull(selector) +} + +@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)")) +public inline fun > 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 > Array.minByOrNull(selector: (T) -> R): T? { if (isEmpty()) return null var minElem = this[0] val lastIndex = this.lastIndex @@ -15317,7 +15263,169 @@ public inline fun > BooleanArray.minBy(selector: (Boolean) -> * * @sample samples.collections.Collections.Aggregates.minBy */ -public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { +@SinceKotlin("1.4") +public inline fun > 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 > 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 > 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 > 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 > 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 > 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 > 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 > CharArray.minByOrNull(selector: (Char) -> R): Char? { if (isEmpty()) return null var minElem = this[0] val lastIndex = this.lastIndex diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 3443f801c6c..40244bc48db 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1736,12 +1736,18 @@ public fun > Iterable.max(): T? { return maxOrNull() } +@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)")) +public inline fun > Iterable.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 > Iterable.maxBy(selector: (T) -> R): T? { +@SinceKotlin("1.4") +public inline fun > Iterable.maxByOrNull(selector: (T) -> R): T? { val iterator = iterator() if (!iterator.hasNext()) return null var maxElem = iterator.next() @@ -2014,12 +2020,18 @@ public fun > Iterable.min(): T? { return minOrNull() } +@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)")) +public inline fun > Iterable.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 > Iterable.minBy(selector: (T) -> R): T? { +@SinceKotlin("1.4") +public inline fun > Iterable.minByOrNull(selector: (T) -> R): T? { val iterator = iterator() if (!iterator.hasNext()) return null var minElem = iterator.next() diff --git a/libraries/stdlib/common/src/generated/_Maps.kt b/libraries/stdlib/common/src/generated/_Maps.kt index 2a24dd68927..3355fee1e74 100644 --- a/libraries/stdlib/common/src/generated/_Maps.kt +++ b/libraries/stdlib/common/src/generated/_Maps.kt @@ -179,14 +179,21 @@ public inline fun Map.forEach(action: (Map.Entry) -> Unit for (element in this) action(element) } +@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)")) +@kotlin.internal.InlineOnly +public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { + 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 > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.maxBy(selector) +public inline fun > Map.maxByOrNull(selector: (Map.Entry) -> R): Map.Entry? { + return entries.maxByOrNull(selector) } /** @@ -309,13 +316,20 @@ public inline fun Map.maxWith(comparator: Comparator> Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { + 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 > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.minBy(selector) +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun > Map.minByOrNull(selector: (Map.Entry) -> R): Map.Entry? { + return entries.minByOrNull(selector) } /** diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index eac3386a2b4..7ac303236e4 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1183,6 +1183,11 @@ public fun > Sequence.max(): T? { return maxOrNull() } +@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)")) +public inline fun > Sequence.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 > Sequence.max(): T? { * * @sample samples.collections.Collections.Aggregates.maxBy */ -public inline fun > Sequence.maxBy(selector: (T) -> R): T? { +@SinceKotlin("1.4") +public inline fun > Sequence.maxByOrNull(selector: (T) -> R): T? { val iterator = iterator() if (!iterator.hasNext()) return null var maxElem = iterator.next() @@ -1487,6 +1493,11 @@ public fun > Sequence.min(): T? { return minOrNull() } +@Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)")) +public inline fun > Sequence.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 > Sequence.min(): T? { * * @sample samples.collections.Collections.Aggregates.minBy */ -public inline fun > Sequence.minBy(selector: (T) -> R): T? { +@SinceKotlin("1.4") +public inline fun > Sequence.minByOrNull(selector: (T) -> R): T? { val iterator = iterator() if (!iterator.hasNext()) return null var minElem = iterator.next() diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 29e86e278a0..564b56d45ba 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1093,12 +1093,18 @@ public fun CharSequence.max(): Char? { return maxOrNull() } +@Deprecated("Use maxByOrNull instead.", ReplaceWith("maxByOrNull(selector)")) +public inline fun > 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 > CharSequence.maxBy(selector: (Char) -> R): Char? { +@SinceKotlin("1.4") +public inline fun > 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 > 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 > CharSequence.minBy(selector: (Char) -> R): Char? { +@SinceKotlin("1.4") +public inline fun > CharSequence.minByOrNull(selector: (Char) -> R): Char? { if (isEmpty()) return null var minElem = this[0] val lastIndex = this.lastIndex diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index dae3e0bb6a4..bbdcedee0b9 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -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 > 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 > 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 > 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 > 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 > 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 > UByteArray.maxBy(selector: (UByte) -> R): * * @sample samples.collections.Collections.Aggregates.maxBy */ -@SinceKotlin("1.3") +@SinceKotlin("1.4") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun > UShortArray.maxBy(selector: (UShort) -> R): UShort? { +public inline fun > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > UByteArray.minBy(selector: (UByte) -> R): * * @sample samples.collections.Collections.Aggregates.minBy */ -@SinceKotlin("1.3") +@SinceKotlin("1.4") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun > UShortArray.minBy(selector: (UShort) -> R): UShort? { +public inline fun > 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 > 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 > UShortArray.minByOrNull(selector: (UShort) -> R): UShort? { if (isEmpty()) return null var minElem = this[0] val lastIndex = this.lastIndex diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index fcb6e8f6f95..7cf9688159b 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -412,63 +412,63 @@ class ArraysTest { expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it*it }) }) } - @Test fun minBy() { - expect(null, { arrayOf().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().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().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().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) } diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index cf1048108a8..9b3321ec175 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -865,41 +865,41 @@ class CollectionTest { expect("B", { listOf("a", "B").asSequence().maxWith(STRING_CASE_INSENSITIVE_ORDER) }) } - @Test fun minBy() { - expect(null, { listOf().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().asSequence().minBy { it } }) - expect(3, { listOf(2, 3).asSequence().minBy { -it } }) + @Test fun minByOrNull() { + expect(null, { listOf().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().asSequence().minByOrNull { it } }) + expect(3, { listOf(2, 3).asSequence().minByOrNull { -it } }) } - @Test fun maxBy() { - expect(null, { listOf().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().asSequence().maxBy { it } }) - expect(2, { listOf(2, 3).asSequence().maxBy { -it } }) + @Test fun maxByOrNull() { + expect(null, { listOf().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().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) } diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index d5c1a540f56..c0ab3cb0f85 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -396,13 +396,13 @@ abstract class IterableTests>(val createFrom: (Array().minBy { it * it } } - expect(1u) { arrayOf(1).minBy { it * it } } - expect(2u) { arrayOf(2, 3).minBy { it * it } } - expect(3uL) { arrayOf(3, 2).minBy { it - 3 } } + fun minByOrNull() { + expect(null) { arrayOf().minByOrNull { it * it } } + expect(1u) { arrayOf(1).minByOrNull { it * it } } + expect(2u) { arrayOf(2, 3).minByOrNull { it * it } } + expect(3uL) { arrayOf(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().maxBy { it + 1 } } - expect(1u) { arrayOf(1).maxBy { it + 1 } } - expect(2u) { arrayOf(2, 3).maxBy { it - 3 } } - expect(3uL) { arrayOf(3, 2).maxBy { it + 1 } } + fun maxByOrNull() { + expect(null) { arrayOf().maxByOrNull { it + 1 } } + expect(1u) { arrayOf(1).maxByOrNull { it + 1 } } + expect(2u) { arrayOf(2, 3).maxByOrNull { it - 3 } } + expect(3uL) { arrayOf(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 }) } diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 76d2df18080..bcede062e20 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -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 diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index afc61dcdb3c..572cf5f42e8 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -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") + 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") - 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)") { @@ -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") - 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)") { includeDefault() include(Maps, CharSequences, ArraysOfUnsigned)