Introduce minOf/maxOf, minOfWith/maxOfWith and their OrNull variants

#KT-38708 Fixed
This commit is contained in:
Ilya Gorbunov
2020-05-21 00:17:01 +03:00
parent 6a24becd1d
commit 7b68de38e1
9 changed files with 6744 additions and 2 deletions
File diff suppressed because it is too large Load Diff
@@ -1793,6 +1793,192 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R):
return maxElem return maxElem
} }
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Float): Float {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Iterable<T>.maxOfOrNull(selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Iterable<T>.maxOfWith(comparator: Comparator<in R>, selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Iterable<T>.maxOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/** /**
* Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements.
*/ */
@@ -1881,6 +2067,192 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R):
return minElem return minElem
} }
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.minOf(selector: (T) -> Float): Float {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Iterable<T>.minOfOrNull(selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection.
*
* @throws NoSuchElementException if the collection is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Iterable<T>.minOfWith(comparator: Comparator<in R>, selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Iterable<T>.minOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/** /**
* Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements.
*/ */
@@ -189,6 +189,110 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.E
return entries.maxBy(selector) return entries.maxBy(selector)
} }
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.maxOf(selector: (Map.Entry<K, V>) -> Double): Double {
return entries.maxOf(selector)
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.maxOf(selector: (Map.Entry<K, V>) -> Float): Float {
return entries.maxOf(selector)
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxOf(selector: (Map.Entry<K, V>) -> R): R {
return entries.maxOf(selector)
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.maxOfOrNull(selector: (Map.Entry<K, V>) -> Double): Double? {
return entries.maxOfOrNull(selector)
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.maxOfOrNull(selector: (Map.Entry<K, V>) -> Float): Float? {
return entries.maxOfOrNull(selector)
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxOfOrNull(selector: (Map.Entry<K, V>) -> R): R? {
return entries.maxOfOrNull(selector)
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R> Map<out K, V>.maxOfWith(comparator: Comparator<in R>, selector: (Map.Entry<K, V>) -> R): R {
return entries.maxOfWith(comparator, selector)
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R> Map<out K, V>.maxOfWithOrNull(comparator: Comparator<in R>, selector: (Map.Entry<K, V>) -> R): R? {
return entries.maxOfWithOrNull(comparator, selector)
}
/** /**
* Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries.
*/ */
@@ -206,6 +310,110 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(selector: (Map.E
return entries.minBy(selector) return entries.minBy(selector)
} }
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.minOf(selector: (Map.Entry<K, V>) -> Double): Double {
return entries.minOf(selector)
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.minOf(selector: (Map.Entry<K, V>) -> Float): Float {
return entries.minOf(selector)
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minOf(selector: (Map.Entry<K, V>) -> R): R {
return entries.minOf(selector)
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.minOfOrNull(selector: (Map.Entry<K, V>) -> Double): Double? {
return entries.minOfOrNull(selector)
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<out K, V>.minOfOrNull(selector: (Map.Entry<K, V>) -> Float): Float? {
return entries.minOfOrNull(selector)
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minOfOrNull(selector: (Map.Entry<K, V>) -> R): R? {
return entries.minOfOrNull(selector)
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each entry in the map.
*
* @throws NoSuchElementException if the map is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R> Map<out K, V>.minOfWith(comparator: Comparator<in R>, selector: (Map.Entry<K, V>) -> R): R {
return entries.minOfWith(comparator, selector)
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each entry in the map or `null` if there are no entries.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <K, V, R> Map<out K, V>.minOfWithOrNull(comparator: Comparator<in R>, selector: (Map.Entry<K, V>) -> R): R? {
return entries.minOfWithOrNull(comparator, selector)
}
/** /**
* Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries.
*/ */
@@ -1248,6 +1248,208 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R):
return maxElem return maxElem
} }
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.maxOf(selector: (T) -> Double): Double {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.maxOf(selector: (T) -> Float): Float {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Sequence<T>.maxOf(selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.maxOfOrNull(selector: (T) -> Double): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.maxOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Sequence<T>.maxOfOrNull(selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Sequence<T>.maxOfWith(comparator: Comparator<in R>, selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Sequence<T>.maxOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/** /**
* Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements.
* *
@@ -1346,6 +1548,208 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R):
return minElem return minElem
} }
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.minOf(selector: (T) -> Double): Double {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.minOf(selector: (T) -> Float): Float {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Sequence<T>.minOf(selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.minOfOrNull(selector: (T) -> Double): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.minOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R : Comparable<R>> Sequence<T>.minOfOrNull(selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the sequence.
*
* @throws NoSuchElementException if the sequence is empty.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Sequence<T>.minOfWith(comparator: Comparator<in R>, selector: (T) -> R): R {
val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each element in the sequence or `null` if there are no elements.
*
* The operation is _terminal_.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <T, R> Sequence<T>.minOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/** /**
* Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements.
* *
@@ -1123,6 +1123,202 @@ public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R):
return maxElem return maxElem
} }
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.maxOf(selector: (Char) -> Double): Double {
if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.maxOf(selector: (Char) -> Float): Float {
if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> CharSequence.maxOf(selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Double): Double? {
if (isEmpty()) return null
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Float): Float? {
if (isEmpty()) return null
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> CharSequence.maxOfOrNull(selector: (Char) -> R): R? {
if (isEmpty()) return null
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (maxValue < v) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R> CharSequence.maxOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/**
* Returns the largest value according to the provided [comparator]
* among all values produced by [selector] function applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R> CharSequence.maxOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? {
if (isEmpty()) return null
var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (comparator.compare(maxValue, v) < 0) {
maxValue = v
}
}
return maxValue
}
/** /**
* Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters.
*/ */
@@ -1171,6 +1367,202 @@ public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R):
return minElem return minElem
} }
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.minOf(selector: (Char) -> Double): Double {
if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.minOf(selector: (Char) -> Float): Float {
if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> CharSequence.minOf(selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double? {
if (isEmpty()) return null
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun CharSequence.minOfOrNull(selector: (Char) -> Float): Float? {
if (isEmpty()) return null
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (v.isNaN()) return v
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R : Comparable<R>> CharSequence.minOfOrNull(selector: (Char) -> R): R? {
if (isEmpty()) return null
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (minValue > v) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each character in the char sequence.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R> CharSequence.minOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/**
* Returns the smallest value according to the provided [comparator]
* among all values produced by [selector] function applied to each character in the char sequence or `null` if there are no characters.
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.internal.InlineOnly
public inline fun <R> CharSequence.minOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? {
if (isEmpty()) return null
var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) {
val v = selector(this[i])
if (comparator.compare(minValue, v) > 0) {
minValue = v
}
}
return minValue
}
/** /**
* Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters.
*/ */
File diff suppressed because it is too large Load Diff
@@ -9,6 +9,7 @@ import test.assertStaticAndRuntimeTypeIs
import kotlin.test.* import kotlin.test.*
import test.collections.behaviors.* import test.collections.behaviors.*
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
import kotlin.math.pow
import kotlin.random.Random import kotlin.random.Random
class CollectionTest { class CollectionTest {
@@ -894,6 +895,63 @@ class CollectionTest {
assertEquals(5, c) assertEquals(5, c)
} }
@Test fun minOf() {
assertEquals(null, emptyList<Int>().minOfOrNull { it } )
assertFailsWith<NoSuchElementException> { emptyList<Int>().minOf { it } }
assertEquals(1, listOf(1).minOf { it })
assertEquals(-3, listOf(2, 3).minOf { -it })
assertEquals("xa", listOf('a', 'b').minOf { "x$it" })
assertEquals(1, listOf("b", "abc").minOf { it.length })
assertEquals(-32.0, listOf(1, 2, 3, 4, 5).minOf { (-2.0).pow(it) })
assertEquals(-32.0F, listOf(1, 2, 3, 4, 5).minOf { (-2.0F).pow(it) })
assertEquals(Double.NaN, listOf(1, -1, 0).minOf { it.toDouble().pow(0.5) })
assertEquals(Float.NaN, listOf(1, -1, 0).minOf { it.toFloat().pow(0.5F) })
}
@Test fun minOfWith() {
val data = listOf("abca", "bcaa", "cabb")
val result = data.minOfWith(compareBy { it.reversed() }) { it.take(3) }
val resultOrNull = data.minOfWithOrNull(compareBy { it.reversed() }) { it.take(3) }
assertEquals("bca", result)
assertEquals(result, resultOrNull)
assertEquals(null, emptyList<Int>().minOfWithOrNull(naturalOrder()) { it })
// TODO: investigate why no unit-coercion happens here and an explicit 'Unit' is required
assertFailsWith<NoSuchElementException> { emptyList<Int>().minOfWith(naturalOrder()) { it }; Unit }
}
@Test fun maxOf() {
assertEquals(null, emptyList<Int>().maxOfOrNull { it } )
assertFailsWith<NoSuchElementException> { emptyList<Int>().maxOf { it } }
assertEquals(1, listOf(1).maxOf { it })
assertEquals(-2, listOf(2, 3).maxOf { -it })
assertEquals("xb", listOf('a', 'b').maxOf { "x$it" })
assertEquals(3, listOf("b", "abc").maxOf { it.length })
assertEquals(16.0, listOf(1, 2, 3, 4, 5).maxOf { (-2.0).pow(it) })
assertEquals(16.0F, listOf(1, 2, 3, 4, 5).maxOf { (-2.0F).pow(it) })
}
@Test fun maxOfWith() {
val data = listOf("abca", "bcaa", "cabb")
val result = data.maxOfWith(compareBy { it.reversed() }) { it.take(3) }
val resultOrNull = data.maxOfWithOrNull(compareBy { it.reversed() }) { it.take(3) }
assertEquals("abc", result)
assertEquals(result, resultOrNull)
assertEquals(null, emptyList<Int>().maxOfWithOrNull(naturalOrder()) { it })
// TODO: investigate why no unit-coercion happens here and an explicit 'Unit' is required
assertFailsWith<NoSuchElementException> { emptyList<Int>().maxOfWith(naturalOrder()) { it }; Unit }
assertEquals(Double.NaN, listOf(1, -1, 0).maxOf { it.toDouble().pow(0.5) })
assertEquals(Float.NaN, listOf(1, -1, 0).maxOf { it.toFloat().pow(0.5F) })
}
@Test fun sum() { @Test fun sum() {
expect(0) { arrayListOf<Int>().sum() } expect(0) { arrayListOf<Int>().sum() }
expect(14) { listOf(2, 3, 9).sum() } expect(14) { listOf(2, 3, 9).sum() }
@@ -544,6 +544,152 @@ object Aggregates : TemplateGroupBase() {
} }
} }
fun f_minMaxOf() = sequence {
fun def(op: String, selectorType: String, nullable: Boolean, orNull: String = "OrNull".ifOrEmpty(nullable)) =
fn("${op}Of$orNull(selector: (T) -> $selectorType)") {
includeDefault()
include(Maps, CharSequences, ArraysOfUnsigned)
} builder {
inlineOnly()
since("1.4")
annotation("@OptIn(kotlin.experimental.ExperimentalTypeInference::class)")
annotation("@OverloadResolutionByLambdaReturnType")
val isFloat = selectorType != "R"
doc {
"""
Returns the ${if (op == "max") "largest" else "smallest"} value among all values produced by [selector] function
applied to each ${f.element} in the ${f.collection}${" or `null` if there are no ${f.element.pluralize()}".ifOrEmpty(nullable)}.
""" +
// """
// If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
// """.ifOrEmpty(isFloat) +
"""
@throws NoSuchElementException if the ${f.collection} is empty.
""".ifOrEmpty(!nullable)
}
if (!isFloat) typeParam("R : Comparable<R>")
returns(selectorType + "?".ifOrEmpty(nullable))
val doOnEmpty = if (nullable) "return null" else "throw NoSuchElementException()"
val acc = op + "Value"
val cmp = if (op == "max") "<" else ">"
body {
"""
val iterator = iterator()
if (!iterator.hasNext()) $doOnEmpty
var $acc = selector(iterator.next())
${"if ($acc.isNaN()) return $acc".ifOrEmpty(isFloat)}
while (iterator.hasNext()) {
val v = selector(iterator.next())
${"if (v.isNaN()) return v".ifOrEmpty(isFloat)}
if ($acc $cmp v) {
$acc = v
}
}
return $acc
"""
}
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
if (isEmpty()) $doOnEmpty
var $acc = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return $acc
${"if ($acc.isNaN()) return $acc".ifOrEmpty(isFloat)}
for (i in 1..lastIndex) {
val v = selector(this[i])
${"if (v.isNaN()) return v".ifOrEmpty(isFloat)}
if ($acc $cmp v) {
$acc = v
}
}
return $acc
"""
}
specialFor(Maps) {
inlineOnly()
body { "return entries.${op}Of$orNull(selector)" }
}
}
for (op in listOf("min", "max"))
for (selectorType in listOf("R", "Float", "Double"))
for (nullable in listOf(false, true))
yield(def(op, selectorType, nullable))
}
fun f_minMaxOfWith() = sequence {
val selectorType = "R"
fun def(op: String, nullable: Boolean, orNull: String = "OrNull".ifOrEmpty(nullable)) =
fn("${op}OfWith$orNull(comparator: Comparator<in R>, selector: (T) -> $selectorType)") {
includeDefault()
include(Maps, CharSequences, ArraysOfUnsigned)
} builder {
inlineOnly()
since("1.4")
annotation("@OptIn(kotlin.experimental.ExperimentalTypeInference::class)")
annotation("@OverloadResolutionByLambdaReturnType")
doc {
"""
Returns the ${if (op == "max") "largest" else "smallest"} value according to the provided [comparator]
among all values produced by [selector] function applied to each ${f.element} in the ${f.collection}${" or `null` if there are no ${f.element.pluralize()}".ifOrEmpty(nullable)}.
""" +
"""
@throws NoSuchElementException if the ${f.collection} is empty.
""".ifOrEmpty(!nullable)
}
typeParam(selectorType)
returns(selectorType + "?".ifOrEmpty(nullable))
val doOnEmpty = if (nullable) "return null" else "throw NoSuchElementException()"
val acc = op + "Value"
val cmp = if (op == "max") "<" else ">"
body {
"""
val iterator = iterator()
if (!iterator.hasNext()) $doOnEmpty
var $acc = selector(iterator.next())
while (iterator.hasNext()) {
val v = selector(iterator.next())
if (comparator.compare($acc, v) $cmp 0) {
$acc = v
}
}
return $acc
"""
}
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
if (isEmpty()) $doOnEmpty
var $acc = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return $acc
for (i in 1..lastIndex) {
val v = selector(this[i])
if (comparator.compare($acc, v) $cmp 0) {
$acc = v
}
}
return $acc
"""
}
specialFor(Maps) {
body { "return entries.${op}OfWith$orNull(comparator, selector)" }
}
}
for (op in listOf("min", "max"))
for (nullable in listOf(false, true))
yield(def(op, nullable))
}
val f_foldIndexed = fn("foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") { val f_foldIndexed = fn("foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") {
includeDefault() includeDefault()
include(CharSequences) include(CharSequences)
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -139,4 +139,6 @@ enum class SequenceClass {
data class Deprecation(val message: String, val replaceWith: String? = null, val level: DeprecationLevel = DeprecationLevel.WARNING) data class Deprecation(val message: String, val replaceWith: String? = null, val level: DeprecationLevel = DeprecationLevel.WARNING)
val forBinaryCompatibility = Deprecation("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) val forBinaryCompatibility = Deprecation("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
data class ThrowsException(val exceptionType: String, val reason: String) data class ThrowsException(val exceptionType: String, val reason: String)
fun String.ifOrEmpty(condition: Boolean): String = if (condition) this else ""