https://kotlinlang.org logo
#stdlib
Title
# stdlib
d

denis090712

02/13/2023, 11:02 AM
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

spand

02/13/2023, 11:51 AM
There is no
SortedMap
in kotlin
j

jw

02/13/2023, 1:13 PM
The stdlib has plenty of extensions on JDK types the same way it also has extensions on JS types and native types
s

spand

02/13/2023, 1:18 PM
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

Marcin Wisniowski

02/20/2023, 11:20 AM
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

spand

02/20/2023, 11:25 AM
A
Map
is no more unordered than a
Collection
or
Set
or
Iterable
which does have a
take
method.
m

Marcin Wisniowski

02/20/2023, 11:27 AM
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.
6 Views