Hello all! I was looking for functionality to sele...
# kontributors
r
Hello all! I was looking for functionality to select 'the n lowest elements in a collection'. I think it doesn't exist in kotlin currently. If not, I could try putting together a PR to add it, but only if whoever the keepers of the relevant library actually want it 😛 How can I find out if this is something that will likely be added or not? Sample unoptimized implementation to demonstrate functionality:
Copy code
fun <T, R : Comparable<R>> Iterable<T>.minBy(n: Int, selector: (T) -> R): List<T> {
    val buffer = sortedSetOf<Pair<R, T>>(compareBy { it.first })
    for (el in this) {
        buffer += selector(el) to el
        if (buffer.size > n) {
            buffer.remove(buffer.last())
        }
    }

    return buffer.map { it.second }
}