mutateAllIndexedNotNull

inline fun <T : MutableCollection<A>, A> T.mutateAllIndexedNotNull(transform: (index: Int, value: A) -> A?): T

Applies transform to each element in the list with the current item index, removing the value if null is returned.

The mutable equivalent to Collection.mapIndexedNotNull.

val p = Person(name = "John", job = Job("Developer", listOf("Kotlin", "Training")))
val newP = p.copy { // mutates the job.teams collection in-place
job.teams.mutateAllIndexedNotNull { i, team -> team.takeIf { i % 2 == 0 } }
}

Note: this function removes in-place only if A is a MutableList, otherwise it clears the collection and re-adds the values.