Introduce new overloads of max and min to fix NaN propagation behavior on JVM.

This commit is contained in:
Ilya Gorbunov
2016-12-28 04:08:05 +03:00
parent 74331f8eaa
commit 0bf2ba8701
11 changed files with 557 additions and 61 deletions
@@ -9720,6 +9720,38 @@ public inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit {
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -9791,8 +9823,10 @@ public fun LongArray.max(): Long? {
public fun FloatArray.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -9804,8 +9838,10 @@ public fun FloatArray.max(): Float? {
public fun DoubleArray.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -10103,6 +10139,38 @@ public fun CharArray.maxWith(comparator: Comparator<in Char>): Char? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -10174,8 +10242,10 @@ public fun LongArray.min(): Long? {
public fun FloatArray.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -10187,8 +10257,10 @@ public fun FloatArray.min(): Float? {
public fun DoubleArray.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -1462,6 +1462,40 @@ public inline fun <T> Iterable<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -1509,6 +1543,40 @@ public fun <T> Iterable<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -841,6 +841,40 @@ public inline fun <T> Sequence<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -888,6 +922,40 @@ public fun <T> Sequence<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -5751,6 +5751,18 @@ public header inline fun BooleanArray.forEachIndexed(action: (Int, Boolean) -> U
*/
public header inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Array<out Double>.max(): Double?
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Array<out Float>.max(): Float?
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -5881,6 +5893,18 @@ public header fun BooleanArray.maxWith(comparator: Comparator<in Boolean>): Bool
*/
public header fun CharArray.maxWith(comparator: Comparator<in Char>): Char?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Array<out Double>.min(): Double?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Array<out Float>.min(): Float?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -765,6 +765,18 @@ public header inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit
*/
public header inline fun <T> Iterable<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Iterable<Double>.max(): Double?
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Iterable<Float>.max(): Float?
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -780,6 +792,18 @@ public header inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T)
*/
public header fun <T> Iterable<T>.maxWith(comparator: Comparator<in T>): T?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Iterable<Double>.min(): Double?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Iterable<Float>.min(): Float?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -492,6 +492,18 @@ public header inline fun <T> Sequence<T>.forEach(action: (T) -> Unit): Unit
*/
public header inline fun <T> Sequence<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Sequence<Double>.max(): Double?
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Sequence<Float>.max(): Float?
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -507,6 +519,18 @@ public header inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T)
*/
public header fun <T> Sequence<T>.maxWith(comparator: Comparator<in T>): T?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Sequence<Double>.min(): Double?
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public header fun Sequence<Float>.min(): Float?
/**
* Returns the smallest element or `null` if there are no elements.
*/
+72
View File
@@ -9803,6 +9803,38 @@ public inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit {
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -9874,8 +9906,10 @@ public fun LongArray.max(): Long? {
public fun FloatArray.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -9887,8 +9921,10 @@ public fun FloatArray.max(): Float? {
public fun DoubleArray.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -10186,6 +10222,38 @@ public fun CharArray.maxWith(comparator: Comparator<in Char>): Char? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -10257,8 +10325,10 @@ public fun LongArray.min(): Long? {
public fun FloatArray.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -10270,8 +10340,10 @@ public fun FloatArray.min(): Float? {
public fun DoubleArray.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -1473,6 +1473,40 @@ public inline fun <T> Iterable<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -1520,6 +1554,40 @@ public fun <T> Iterable<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -860,6 +860,40 @@ public inline fun <T> Sequence<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -907,6 +941,40 @@ public fun <T> Sequence<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -827,6 +827,8 @@ public final class kotlin/collections/ArraysKt {
public static final fun max ([I)Ljava/lang/Integer;
public static final fun max ([J)Ljava/lang/Long;
public static final fun max ([Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun max ([Ljava/lang/Double;)Ljava/lang/Double;
public static final fun max ([Ljava/lang/Float;)Ljava/lang/Float;
public static final fun max ([S)Ljava/lang/Short;
public static final fun maxBy ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte;
public static final fun maxBy ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character;
@@ -853,6 +855,8 @@ public final class kotlin/collections/ArraysKt {
public static final fun min ([I)Ljava/lang/Integer;
public static final fun min ([J)Ljava/lang/Long;
public static final fun min ([Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun min ([Ljava/lang/Double;)Ljava/lang/Double;
public static final fun min ([Ljava/lang/Float;)Ljava/lang/Float;
public static final fun min ([S)Ljava/lang/Short;
public static final fun minBy ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte;
public static final fun minBy ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character;
@@ -1501,9 +1505,13 @@ public final class kotlin/collections/CollectionsKt {
public static final fun mapNotNullTo (Ljava/lang/Iterable;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection;
public static final fun mapTo (Ljava/lang/Iterable;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection;
public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Comparable;
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 maxWith (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Comparable;
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 minWith (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun minus (Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List;
@@ -2098,9 +2106,13 @@ public final class kotlin/sequences/SequencesKt {
public static final fun mapNotNullTo (Lkotlin/sequences/Sequence;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection;
public static final fun mapTo (Lkotlin/sequences/Sequence;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection;
public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable;
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 maxWith (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable;
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 minWith (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun minus (Lkotlin/sequences/Sequence;Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
@@ -137,36 +137,65 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("min()") {
doc { f -> "Returns the smallest ${f.element} or `null` if there are no ${f.element.pluralize()}." }
returns("T?")
exclude(PrimitiveType.Boolean)
typeParam("T : Comparable<T>")
body {
"""
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (min > e) min = e
templates addAll listOf("min", "max").flatMap { op ->
val genericSpecializations = PrimitiveType.numericPrimitives.filterNot { it.isIntegral() } + listOf(null)
listOf(
Iterables to genericSpecializations,
Sequences to genericSpecializations,
ArraysOfObjects to genericSpecializations,
ArraysOfPrimitives to (PrimitiveType.defaultPrimitives - PrimitiveType.Boolean),
CharSequences to setOf(null)
).map { (f, primitives) -> primitives.map { primitive ->
f("$op()") {
val isFloat = primitive?.isIntegral() == false
val isGeneric = f in listOf(Iterables, Sequences, ArraysOfObjects)
only(f)
typeParam("T : Comparable<T>")
if (primitive != null) {
onlyPrimitives(f, primitive)
if (isFloat && isGeneric)
since("1.1")
}
doc { f -> "Returns the ${if (op == "max") "largest" else "smallest"} ${f.element} or `null` if there are no ${f.element.pluralize()}." }
returns("T?")
body {
if (f == ArraysOfObjects || f == ArraysOfPrimitives || f == CharSequences) {
"""
if (isEmpty()) return null
var $op = this[0]
${if (isFloat) "if ($op.isNaN()) return $op" else "\\"}
for (i in 1..lastIndex) {
val e = this[i]
${if (isFloat) "if (e.isNaN()) return e" else "\\"}
if ($op ${if (op == "max") "<" else ">"} e) $op = e
}
return $op
"""
}
else {
"""
val iterator = iterator()
if (!iterator.hasNext()) return null
var $op = iterator.next()
${if (isFloat) "if ($op.isNaN()) return $op" else "\\"}
while (iterator.hasNext()) {
val e = iterator.next()
${if (isFloat) "if (e.isNaN()) return e" else "\\"}
if ($op ${if (op == "max") "<" else ">"} e) $op = e
}
return $op
"""
}.replace(Regex("""^\s+\\\n""", RegexOption.MULTILINE), "") // trim lines ending with \
}
}
return min
"""
}
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
"""
}
}
}}
}.flatten()
templates add f("minBy(selector: (T) -> R)") {
inline(true)
@@ -242,39 +271,6 @@ fun aggregates(): List<GenericFunction> {
body(Maps) { "return entries.minWith(comparator)" }
}
templates add f("max()") {
doc { f -> "Returns the largest ${f.element} or `null` if there are no ${f.element.pluralize()}." }
returns("T?")
exclude(PrimitiveType.Boolean)
typeParam("T : Comparable<T>")
body {
"""
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (max < e) max = e
}
return max
"""
}
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
"""
}
}
templates add f("maxBy(selector: (T) -> R)") {
inline(true)