Unify minBy and maxBy for Maps with other families.
This commit is contained in:
@@ -135,41 +135,17 @@ public inline fun <K, V> Map<K, V>.forEach(action: (Map.Entry<K, V>) -> 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 <K, V, R : Comparable<R>> Map<K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
public inline fun <K, V, R : Comparable<R>> Map<K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
||||||
val iterator = iterator()
|
return entries.maxBy(selector)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 <K, V, R : Comparable<R>> Map<K, V>.minBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
public inline fun <K, V, R : Comparable<R>> Map<K, V>.minBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
|
||||||
val iterator = iterator()
|
return entries.minBy(selector)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -181,7 +181,6 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
inline(true)
|
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()}." }
|
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<R>")
|
typeParam("R : Comparable<R>")
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
@@ -220,33 +219,7 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
return minElem
|
return minElem
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
body(Maps) { "return entries.minBy(selector)" }
|
||||||
|
|
||||||
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<R>")
|
|
||||||
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
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("max()") {
|
templates add f("max()") {
|
||||||
@@ -287,7 +260,6 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
inline(true)
|
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()}." }
|
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<R>")
|
typeParam("R : Comparable<R>")
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
@@ -326,33 +298,7 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
return maxElem
|
return maxElem
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
body(Maps) { "return entries.maxBy(selector)" }
|
||||||
|
|
||||||
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<R>")
|
|
||||||
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
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("fold(initial: R, operation: (R, T) -> R)") {
|
templates add f("fold(initial: R, operation: (R, T) -> R)") {
|
||||||
|
|||||||
Reference in New Issue
Block a user