arekolek
08/14/2017, 6:10 PMList<T>
or Iterator<T>
and does what you’d like, so I doubt there is one.
Here are two ways I could think of to implement it though:
inline fun <T, R : Comparable<R>> Iterable<T>.allMaxBy(selector: (T) -> R): List<T>? {
return map(selector).max()?.let { best -> filter { selector(it) == best } }
}
inline fun <T, R : Comparable<R>> Iterable<T>.allMaxBy(selector: (T) -> R): List<T>? {
return groupBy(selector).run { get(keys.max()) }
}
supaham
08/14/2017, 6:12 PMarekolek
08/14/2017, 6:21 PMallMaxBy
function altogether), but the first version should actually perform better (it doesn’t allocate as much stuff as the second version)