Rob Elliot
01/16/2023, 6:01 PMList<T>.take(n: Int): List<T>
only Iterable<T>.take(n: Int): List<T>
which constructs a new ArrayList and copies elements in.
Wouldn't this be more efficient on a List, as it could just return a view?
fun List<T>.take(n: Int): List<T> = subList(0, n)
(And of course the same for drop
)jw
01/16/2023, 6:05 PMRob Elliot
01/16/2023, 6:06 PMephemient
01/16/2023, 11:06 PMRob Elliot
01/17/2023, 9:55 AMImmutableList<T> : List<T>
interface as a parallel to MutableList
CLOVIS
01/17/2023, 10:59 AM.take
be an extension function was a good idea. If it were a regular interface method with default implementation, immutable list implementations could have overriden it to provide a subList-based implementation when it's safeephemient
01/17/2023, 11:54 AMplus
and minus
currently, no reason take
etc. couldn't use the same pattern. does have the downside of being difficult to tell what is happening locally thoughCLOVIS
01/17/2023, 12:05 PMilya.gorbunov
01/17/2023, 7:15 PMtake
was implemented as subList
(given that the take
contract is respected, for example, by doing so only for immutable lists), it could lead to retaining more objects from garbage collection than necessary.
Suppose you have a large list, but only temporary, and you want to take and store for a long period a list of only several first elements from it. If you use take
for that, and take
is implemented as a view, it would retain the entire large list in memory when only several elements of it are needed.CLOVIS
01/17/2023, 7:17 PMString.substring
retains the original string 🙄jw
01/17/2023, 7:17 PMilya.gorbunov
01/17/2023, 7:17 PMsubstring
no longer does that.CLOVIS
01/17/2023, 7:18 PMephemient
01/18/2023, 6:08 AM