minOfOrNull

inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double?(source)
inline fun CharSequence.minOfOrNull(selector: (Char) -> Float): Float?(source)

Returns the smallest value among all values produced by selector function applied to each character in the char sequence or null if the char sequence is empty.

If any of values produced by selector function is NaN, the returned result is NaN.

Since Kotlin

1.4

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   data class Rectangle(val width: Double, val height: Double) {
    val aspectRatio: Double get() = if (height != 0.0) width / height else Double.NaN
}

// The largest and smallest width-to-height ratios
val rectangles = listOf(
    Rectangle(15.0, 10.0),
    Rectangle(25.0, 20.0),
    Rectangle(40.0, 30.0),
)
println(rectangles.maxOf { it.aspectRatio }) // 1.5
println(rectangles.minOf { it.aspectRatio }) // 1.25

// Aspect ratio of a point (0.0 by 0.0) is NaN, hence the result is NaN
val rectanglesAndPoint = rectangles + Rectangle(0.0, 0.0)
println(rectanglesAndPoint.maxOf { it.aspectRatio }) // NaN
println(rectanglesAndPoint.minOf { it.aspectRatio }) // NaN

val emptyList = emptyList<Rectangle>()

// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.aspectRatio } // will fail with NoSuchElementException
// emptyList.minOf { it.aspectRatio } // will fail with NoSuchElementException

// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.aspectRatio }) // null
println(emptyList.minOfOrNull { it.aspectRatio }) // null 
   //sampleEnd
}

inline fun <R : Comparable<R>> CharSequence.minOfOrNull(selector: (Char) -> R): R?(source)

Returns the smallest value among all values produced by selector function applied to each character in the char sequence or null if the char sequence is empty.

If multiple characters produce the minimal value, this function returns the first of those values.

Since Kotlin

1.4

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // The length of the longest and shortest names
val names = listOf("Alice", "Bob", "Carol")
println(names.maxOf { it.length }) // 5
println(names.minOf { it.length }) // 3

val emptyList = emptyList<String>()

// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.length } // will fail with NoSuchElementException
// emptyList.minOf { it.length } // will fail with NoSuchElementException

// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.length }) // null
println(emptyList.minOfOrNull { it.length }) // null 
   //sampleEnd
}