dropAt for List

Thoughts on a function dropAt for type List that mimics removeAt for mutableLists?

i.e it doesn’t modify the original list (as drops don’t), but it returns a List where the item at that index is removed.
Usage:

val names = "One Two Three Four".split(" ")
val newNames = names.dropAt(0)

names.forEach { print("$it ") } // => One Two Three Four
newNames.forEach { print("$it ") } // => Two Three Four

Hi there, you could write an extension function like so:

fun List<*>.dropAt(index: Int) = this.filterIndexed { i, _ ->
   i != index
}

as this is very easily implemented I don’t see the benefit of adding a function like that to the standard library

Based on that argument you would need to remove most of the extension functions for List from the stdlib.

My concern would be that people would use dropAt on MutableLists when they meant to use removeAt. Also I don’t know about the fact that drops don’t modify the original, but I don’t think this is a good enough argument for not adding this function.

1 Like

The MutableList also has drop, dropWhile, etc. which all return a immutable List and don’t modify the original MutableList. removeAt on the other hand does return the removed element. I don’t think it would be much of a problem to distinguish the two.

You got a point that there are many convenience methods that could be removed as they are easy to implement. I guess if enough people need a function then there is indeed a benefit to including even simple extensions.

@tylerwbrown what use case do you have in mind for this?

You guys pretty much nailed it. dropAt is like the other drop functions that exist. They can all be replaced with simple extension functions, but I think they’re standard enough to exist in the std lib.