Was trying to look it up, but didn't find anything related:
What about adding std lib function to cut List into two sublists before/after certain index?
I know this could be easily implemented but found this to be not so rare use case after all.
Take something like this for example:
fun <T> List<T>.cut(cutIndex: Int): Pair<List<T>, List<T>> {
if (cutIndex !in indices) throw IndexOutOfBoundsException("cutIndex $cutIndex out of bounds for List indices $indices")
return subList(0, cutIndex) to subList(cutIndex, size)
}
fun <T> List<T>.cutInHalf(): Pair<List<T>, List<T>> {
val midIndex = size / 2
return subList(0, midIndex) to subList(midIndex, size)
}
Having
val sampleList = listOf(1, 8, 3, 9, 12, 4)
sampleList.cutInHalf()
would result in
([1, 8, 3], [9, 12, 4])
sampleList.cut(4)
would result in
([1, 8, 3, 9], [12, 4])
etc.
cutInHalf
wouldn't be ideal for Lists with odd number of elements, but I guess that should be obvious