I need to remove an item from a list without mutating it.
I can write my own:
fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
take(index) + drop(index + 1)
but it feels like this should be in the standard library.
Is there such a function?
You could use filterIndexed
:
fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
filterIndexed { i, _ -> i != index }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With