Sololearn: Learn to Code
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
23rd Dec 2022, 8:33 AM
Tibor Santa
Tibor Santa - avatar
+ 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
23rd Dec 2022, 3:21 AM
Ion Kare
Ion Kare - avatar