From 286e81992a8c3d3bb755a8afe1ebe1fc549631a1 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 12 Apr 2017 17:11:11 +0700 Subject: [PATCH] stdlib: Add missing extensions for collections --- .../kotlin/kotlin/collections/Collections.kt | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index fbd0798813e..e1a0e8a01f0 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -1500,6 +1500,20 @@ public inline fun >> Iterable.gr return destination } +/** + * Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations + * using the specified [keySelector] function to extract a key from each element. + * + * @sample samples.collections.Collections.Transformations.groupingByEachCount + */ +@SinceKotlin("1.1") +public inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping { + return object : Grouping { + override fun sourceIterator(): Iterator = this@groupingBy.iterator() + override fun keyOf(element: T): K = keySelector(element) + } +} + /** * Returns a list containing the results of applying the given [transform] function * to each element in the original collection. @@ -1784,6 +1798,44 @@ public inline fun Iterable.forEachIndexed(action: (Int, T) -> Unit): Unit for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + * + * If any of elements is `NaN` returns `NaN`. + */ +@SinceKotlin("1.1") +public fun Iterable.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. + * + * If any of elements is `NaN` returns `NaN`. + */ +@SinceKotlin("1.1") +public fun Iterable.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. */ @@ -1831,6 +1883,44 @@ public fun Iterable.maxWith(comparator: Comparator): T? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + * + * If any of elements is `NaN` returns `NaN`. + */ +@SinceKotlin("1.1") +public fun Iterable.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. + * + * If any of elements is `NaN` returns `NaN`. + */ +@SinceKotlin("1.1") +public fun Iterable.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. */