New course! Every coder should learn Generative AI!
Try a free lesson+ 6
The function signature of forEach is like this:
forEach(action: (T) -> Unit)
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html
It means that it takes a function as argument, which doesn't return anything. So it is purely for side effects.
When we use forEach on a collection, this function is executed on each element.
We can write similar functions with Kotlin easily. The trick is if the function has multiple arguments, the last one can be a trailing lambda, then we can use this syntax with braces where we define our own custom anonymous function what must be executed.
Example:
https://code.sololearn.com/cgRcXqzyyn9z/?ref=app
Further study:
https://kotlinlang.org/docs/lambdas.html
+ 2
Closures is what you might be looking for, kotlin has lots of ways to do things , kinda similar to rust , just read the kotlin docs on foreach