Higher-order functions
Kotlin allows you to write functions that return another function or that take functions as arguments. This is a very powerful feature and is leveraged heavily across the Kotlin ecosystem. One way in which higher-order functions represent a large change from Java is in their ability to replace Single Abstract Method types (SAM-types) with function arguments and passed lambdas. It's no longer required that you define an interface for simple callbacks that could be treated as a function. You can see one example of this in the following snippet.
In the same example from the Kotlin Standard Library, we see that the forEach function takes another function as an argument. That function will define what should happen when ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access