Kotlin foreach return list

Viewed 75

simple question, is there a method for a collection to apply action on every element (like foreach) but who return the list

I can write it myself :

public inline fun <T> Iterable<T>.myforEach(action: (T) -> Unit): Iterable<T> {
   for (element in this) action(element)
        
   return this
}

But it must already exist

1 Answers

There is onEach for this:

val sameList = list.onEach { doStuff() }
Related