mutateAllNotNull

inline fun <T : MutableCollection<A>, A> T.mutateAllNotNull(transform: (A) -> A?): T

Applies transform to each element in the collection, removing the item if null is returned.

The mutable equivalent to Collection.mapNotNull.

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


inline fun <A> MutableList<A>.mutateAllNotNull(transform: (A) -> A?): MutableList<A>

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

The mutable equivalent to List.mapNotNull.

val p = Person(name = "John", job = Job("Developer", listOf("Kotlin", "Training")))
val newP = p.copy { // mutates the job.teams collection in-place
job.teams.mutateAllNotNull { it.takeUnless { it.startsWith("admin-") } }
}