How do you think would it be useful to have an ext...
# stdlib
d
How do you think would it be useful to have an extension function that takes first N elements in a SortedMap? Something like this:
Copy code
fun <K, V> SortedMap<K, V>.take(count: Int): SortedMap<K, V> {
    if (size <= count) return this
    val found = keys.elementAt(count)
    return headMap(found)
}
also, it is possible to implement
drop
function in the same fashion My case was to take a snapshot of the original tree of depth N.
s
There is no
SortedMap
in kotlin
j
The stdlib has plenty of extensions on JDK types the same way it also has extensions on JS types and native types
s
Yeah sure. It just seems a few step away to ask for specialized
take
and
drop
on a jvm type when they arent present on
Map
m
They aren’t present on
Map
because the concept of taking the first 5 elements doesn’t make sense on an unordered map. It makes sense on
SortedMap
, so it’s a valid request.
s
A
Map
is no more unordered than a
Collection
or
Set
or
Iterable
which does have a
take
method.
m
Fair point, all the more reason of having it on SortedMap then. Still, it’s probably not a common enough use-case to include in the stdlib.