This question is kinda all over the place (stdlib design, code golf), so apologies in advance.
We have a list of size >= 2 and we’d like to peel off the first two values into separate variables and create a new list from the remaining values, essentially something like this:
val list = listOf(1, 2, 3) val first = list val second = list val rest = list.drop(2)
(What we actually did was introduce a
List.splitAt(idx: Int): Pair<List, List> extension method:)
val list = listOf(1, 2, 3) val (meta, rest) = list.splitAt(2) val (first, second) = meta
But this isn’t that much better.
What we’d really like is this:
val list = listOf(1, 2, 3) val ((first, second), *rest) = list
but this isn’t possible right now in 1.1.
What are people’s thoughts? Is there a cleaner idiomatic way to do what we’re doing?