Eric Joseph
08/11/2025, 5:52 PMNicolai C.
08/11/2025, 6:04 PMasSequence
function. Kotlin generally prefers immutable collections, and non-mutating functions.
Or perhaps some of the remove
-like functions on MutableCollection
would be of use to you?Eric Joseph
08/11/2025, 6:48 PMRuckus
08/11/2025, 7:12 PMdropWhile
isn't the correct analogy, as it will only drop till the return if false. I'm not aware of a built in function, but if you need one at a time processing (which I'm assuming is why you aren't happy with your drain
solution) you can define your own pretty easy:
inline fun <T> MutableList<T>.processAll(action: (T) -> Unit) {
while (!isEmpty()) action(removeFirst())
}
(Also, if the order doesn't matter and the list can get pretty big, removeLast()
will likely be more performant than removeFirst()
.)Ruckus
08/11/2025, 7:13 PMDaniel Pitts
08/11/2025, 7:13 PM.removeIf
method?Eric Joseph
08/11/2025, 7:26 PMEric Joseph
08/11/2025, 7:27 PMRuckus
08/11/2025, 7:30 PMdropWhile
and removeIf
are not the same. For example, if you have the following list:
val list = mutableListOf(1, 2, 10, 3)
you will see the following:
list.dropWhile { it < 5 } == listOf(10, 3)
list.removeIf { it < 5 } == listOf(10)
Daniel Pitts
08/11/2025, 7:31 PMDaniel Pitts
08/11/2025, 7:32 PMEric Joseph
08/11/2025, 7:45 PMdropWhile
doesn’t modify the list in place; it returns a new one. In other contexts, it works fine too.gildor
08/12/2025, 8:27 AMDaniel Pitts
08/12/2025, 2:26 PMgildor
08/13/2025, 5:03 AM