Working with higher-order functions
Kotlin is designed to provide first-class support for operating on functions. For example, we are able to easily pass functions as parameters to a function. We can also create a function that can return another function. This kind of a function is called a higher-order function. This powerful feature helps to write a functional style code easily. The possibility to return a function instead of a value makes along with the ability to pass a function instance to an other function as an argument, makes it possible to defer computations and to shape code cleanly. In this recipe, we are going to implement a helper function that is going to measure the execution time of other functions passed to it as an argument.
How to do it...
Implement the measureTime
function:
fun measureTime(block: () -> Unit): Long { val start = System.currentTimeMillis() block() val end = System.currentTimeMillis() return end - start }
How it works...
The measureTime()
function takes...