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 each character in CharSequence is iterated over:

// 'action' is a function that is called for each char
// in the CharSequence
inline fun CharSequence.forEach(action: (Char) -> Unit)

Because of Kotlin's support for higher-order functions, it's not necessary to pass any type of class or interface as a callback: instead, pure functions can be used.