Lambda functions

Lambda functions are nothing but functions without a name. We used to call them anonymous functions. A function is basically passed into a parameter of a function call, but as an expression. They are very useful. They save us a lot of time by not writing specific functions in an abstract class or interface.

Lambda usage can be as simple the following code snippet, where it seems like we are simply binding a block an invocation of the helloKotlin function:

    fun main(args: Array<String>) {      val helloKotlin={println("Hello from KotlinBlueprints team!")}      helloKotlin()    }

At the same time, lambda can be a bit complex as well, just like the following code block:

 

    fun <T> lock(lock: Lock, body: () -> T): T {      lock.lock()      try {        

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.