October 2019
Intermediate to advanced
434 pages
11h 54m
English
Another important feature of Kotlin functions is the ability to pass functions as arguments to other functions. This is partially what makes up the term higher-order functions, the other component being the ability to return functions from other functions.
A function argument can be added to a function the same way as any other argument type:
class GreetingViewModel { private val greetings = listOf("Hey", "Hi", "What's up?") fun printGreetings(filter: (String) -> Boolean) { greetings .filter(filter) .forEach { greeting -> println(greeting) } }}
In this example, we've added a printGreetings function to print out all the available greetings. The function takes a function parameter with a signature that takes a String ...
Read now
Unlock full access