@@ -1797,6 +1797,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R):
17971797 * Returns the largest value among all values produced by [selector] function
17981798 * applied to each element in the collection.
17991799 *
1800+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
1801+ *
18001802 * @throws NoSuchElementException if the collection is empty.
18011803 */
18021804@SinceKotlin(" 1.4" )
@@ -1807,13 +1809,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
18071809 val iterator = iterator()
18081810 if (! iterator.hasNext()) throw NoSuchElementException ()
18091811 var maxValue = selector(iterator.next())
1810- if (maxValue.isNaN()) return maxValue
18111812 while (iterator.hasNext()) {
18121813 val v = selector(iterator.next())
1813- if (v.isNaN()) return v
1814- if (maxValue < v) {
1815- maxValue = v
1816- }
1814+ maxValue = maxOf(maxValue, v)
18171815 }
18181816 return maxValue
18191817}
@@ -1822,6 +1820,8 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
18221820 * Returns the largest value among all values produced by [selector] function
18231821 * applied to each element in the collection.
18241822 *
1823+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
1824+ *
18251825 * @throws NoSuchElementException if the collection is empty.
18261826 */
18271827@SinceKotlin(" 1.4" )
@@ -1832,13 +1832,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Float): Float {
18321832 val iterator = iterator()
18331833 if (! iterator.hasNext()) throw NoSuchElementException ()
18341834 var maxValue = selector(iterator.next())
1835- if (maxValue.isNaN()) return maxValue
18361835 while (iterator.hasNext()) {
18371836 val v = selector(iterator.next())
1838- if (v.isNaN()) return v
1839- if (maxValue < v) {
1840- maxValue = v
1841- }
1837+ maxValue = maxOf(maxValue, v)
18421838 }
18431839 return maxValue
18441840}
@@ -1859,7 +1855,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
18591855 var maxValue = selector(iterator.next())
18601856 while (iterator.hasNext()) {
18611857 val v = selector(iterator.next())
1862-
18631858 if (maxValue < v) {
18641859 maxValue = v
18651860 }
@@ -1870,6 +1865,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
18701865/* *
18711866 * Returns the largest value among all values produced by [selector] function
18721867 * applied to each element in the collection or `null` if there are no elements.
1868+ *
1869+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
18731870 */
18741871@SinceKotlin(" 1.4" )
18751872@OptIn(kotlin.experimental.ExperimentalTypeInference ::class )
@@ -1879,20 +1876,18 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double?
18791876 val iterator = iterator()
18801877 if (! iterator.hasNext()) return null
18811878 var maxValue = selector(iterator.next())
1882- if (maxValue.isNaN()) return maxValue
18831879 while (iterator.hasNext()) {
18841880 val v = selector(iterator.next())
1885- if (v.isNaN()) return v
1886- if (maxValue < v) {
1887- maxValue = v
1888- }
1881+ maxValue = maxOf(maxValue, v)
18891882 }
18901883 return maxValue
18911884}
18921885
18931886/* *
18941887 * Returns the largest value among all values produced by [selector] function
18951888 * applied to each element in the collection or `null` if there are no elements.
1889+ *
1890+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
18961891 */
18971892@SinceKotlin(" 1.4" )
18981893@OptIn(kotlin.experimental.ExperimentalTypeInference ::class )
@@ -1902,13 +1897,9 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Float): Float? {
19021897 val iterator = iterator()
19031898 if (! iterator.hasNext()) return null
19041899 var maxValue = selector(iterator.next())
1905- if (maxValue.isNaN()) return maxValue
19061900 while (iterator.hasNext()) {
19071901 val v = selector(iterator.next())
1908- if (v.isNaN()) return v
1909- if (maxValue < v) {
1910- maxValue = v
1911- }
1902+ maxValue = maxOf(maxValue, v)
19121903 }
19131904 return maxValue
19141905}
@@ -1927,7 +1918,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOfOrNull(selector: (T) -
19271918 var maxValue = selector(iterator.next())
19281919 while (iterator.hasNext()) {
19291920 val v = selector(iterator.next())
1930-
19311921 if (maxValue < v) {
19321922 maxValue = v
19331923 }
@@ -2071,6 +2061,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R):
20712061 * Returns the smallest value among all values produced by [selector] function
20722062 * applied to each element in the collection.
20732063 *
2064+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
2065+ *
20742066 * @throws NoSuchElementException if the collection is empty.
20752067 */
20762068@SinceKotlin(" 1.4" )
@@ -2081,13 +2073,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
20812073 val iterator = iterator()
20822074 if (! iterator.hasNext()) throw NoSuchElementException ()
20832075 var minValue = selector(iterator.next())
2084- if (minValue.isNaN()) return minValue
20852076 while (iterator.hasNext()) {
20862077 val v = selector(iterator.next())
2087- if (v.isNaN()) return v
2088- if (minValue > v) {
2089- minValue = v
2090- }
2078+ minValue = minOf(minValue, v)
20912079 }
20922080 return minValue
20932081}
@@ -2096,6 +2084,8 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
20962084 * Returns the smallest value among all values produced by [selector] function
20972085 * applied to each element in the collection.
20982086 *
2087+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
2088+ *
20992089 * @throws NoSuchElementException if the collection is empty.
21002090 */
21012091@SinceKotlin(" 1.4" )
@@ -2106,13 +2096,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Float): Float {
21062096 val iterator = iterator()
21072097 if (! iterator.hasNext()) throw NoSuchElementException ()
21082098 var minValue = selector(iterator.next())
2109- if (minValue.isNaN()) return minValue
21102099 while (iterator.hasNext()) {
21112100 val v = selector(iterator.next())
2112- if (v.isNaN()) return v
2113- if (minValue > v) {
2114- minValue = v
2115- }
2101+ minValue = minOf(minValue, v)
21162102 }
21172103 return minValue
21182104}
@@ -2133,7 +2119,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
21332119 var minValue = selector(iterator.next())
21342120 while (iterator.hasNext()) {
21352121 val v = selector(iterator.next())
2136-
21372122 if (minValue > v) {
21382123 minValue = v
21392124 }
@@ -2144,6 +2129,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
21442129/* *
21452130 * Returns the smallest value among all values produced by [selector] function
21462131 * applied to each element in the collection or `null` if there are no elements.
2132+ *
2133+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
21472134 */
21482135@SinceKotlin(" 1.4" )
21492136@OptIn(kotlin.experimental.ExperimentalTypeInference ::class )
@@ -2153,20 +2140,18 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double?
21532140 val iterator = iterator()
21542141 if (! iterator.hasNext()) return null
21552142 var minValue = selector(iterator.next())
2156- if (minValue.isNaN()) return minValue
21572143 while (iterator.hasNext()) {
21582144 val v = selector(iterator.next())
2159- if (v.isNaN()) return v
2160- if (minValue > v) {
2161- minValue = v
2162- }
2145+ minValue = minOf(minValue, v)
21632146 }
21642147 return minValue
21652148}
21662149
21672150/* *
21682151 * Returns the smallest value among all values produced by [selector] function
21692152 * applied to each element in the collection or `null` if there are no elements.
2153+ *
2154+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
21702155 */
21712156@SinceKotlin(" 1.4" )
21722157@OptIn(kotlin.experimental.ExperimentalTypeInference ::class )
@@ -2176,13 +2161,9 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Float): Float? {
21762161 val iterator = iterator()
21772162 if (! iterator.hasNext()) return null
21782163 var minValue = selector(iterator.next())
2179- if (minValue.isNaN()) return minValue
21802164 while (iterator.hasNext()) {
21812165 val v = selector(iterator.next())
2182- if (v.isNaN()) return v
2183- if (minValue > v) {
2184- minValue = v
2185- }
2166+ minValue = minOf(minValue, v)
21862167 }
21872168 return minValue
21882169}
@@ -2201,7 +2182,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOfOrNull(selector: (T) -
22012182 var minValue = selector(iterator.next())
22022183 while (iterator.hasNext()) {
22032184 val v = selector(iterator.next())
2204-
22052185 if (minValue > v) {
22062186 minValue = v
22072187 }
0 commit comments