From 33967a09f678ff4f2282a6aa37e1bd861269a3ee Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 30 Nov 2015 19:19:40 +0300 Subject: [PATCH] Unify minBy and maxBy for Maps with other families. --- libraries/stdlib/src/generated/_Maps.kt | 32 ++-------- .../src/templates/Aggregates.kt | 58 +------------------ 2 files changed, 6 insertions(+), 84 deletions(-) diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt index c2a49f3abda..ebdfe22ab39 100644 --- a/libraries/stdlib/src/generated/_Maps.kt +++ b/libraries/stdlib/src/generated/_Maps.kt @@ -135,41 +135,17 @@ public inline fun Map.forEach(action: (Map.Entry) -> Unit): U } /** - * Returns the first map entry yielding the largest value of the given function or `null` if there are no entries. + * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. */ public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem + return entries.maxBy(selector) } /** - * Returns the first map entry yielding the smallest value of the given function or `null` if there are no entries. + * Returns the first entry yielding the smallest value of the given function or `null` if there are no entries. */ public inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem + return entries.minBy(selector) } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 1fa549f4cff..48b40ec1027 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -181,7 +181,6 @@ fun aggregates(): List { inline(true) doc { f -> "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element.pluralize()}." } - typeParam("T") typeParam("R : Comparable") returns("T?") body { @@ -220,33 +219,7 @@ fun aggregates(): List { return minElem """ } - } - - templates add f("minBy(selector: (T) -> R)") { - inline(true) - - only(Maps) - doc { "Returns the first map entry yielding the smallest value of the given function or `null` if there are no entries." } - typeParam("R : Comparable") - returns("T?") - body { - """ - val iterator = iterator() - if (!iterator.hasNext()) return null - - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem - """ - } + body(Maps) { "return entries.minBy(selector)" } } templates add f("max()") { @@ -287,7 +260,6 @@ fun aggregates(): List { inline(true) doc { f -> "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element.pluralize()}." } - typeParam("T") typeParam("R : Comparable") returns("T?") body { @@ -326,33 +298,7 @@ fun aggregates(): List { return maxElem """ } - } - - templates add f("maxBy(selector: (T) -> R)") { - inline(true) - - only(Maps) - doc { "Returns the first map entry yielding the largest value of the given function or `null` if there are no entries." } - typeParam("R : Comparable") - returns("T?") - body { - """ - val iterator = iterator() - if (!iterator.hasNext()) return null - - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem - """ - } + body(Maps) { "return entries.maxBy(selector)" } } templates add f("fold(initial: R, operation: (R, T) -> R)") {