Working with anonymous functions
In Kotlin, we can have functions as expressions by creating lambdas. Lambdas are function literals—that is, they are not declared as they are expressions and can be passed as parameters. However, we cannot declare return types in lambdas. Although the return type is inferred automatically by Kotlin compiler in most cases, for cases where it cannot be inferred on its own or it needs to be declared explicitly, we use anonymous functions. In this recipe, we will see how to use anonymous functions.
Getting ready
You need to install a preferred development environment that compiles and runs Kotlin. You can also use the command line for this purpose, for which you need Kotlin compiler installed, along with JDK. I am using an online IDE at https://try.kotlinlang.org/ to compile and run my Kotlin code for this recipe. You can also use IntelliJ IDEA for the development environment.
How to do it...
In the following steps, we will learn about anonymous functions with the...