Lambda in Kotlin-Android

In this post we will see Lambda in detail, we will start from very basic structure of lambda and will explore its different writing style and practical usage.

so sit back, relax and grab a bag of popcorns(the hot ones)…

Let’s start

Lambda introduction

Definition: Lambda is a function without a name.

Just like function it has input parameters, return type and body but it does not have name.

The basic structure of lambda is

val lambdaName : (Int, Int) -> Unit = { arg1, arg2 -> //codeHere }
fun sum(a: Int, b: Int): Int {
    return a + b
}

equivalent lambda of above function will be->

val sum: (Int, Int) -> Int = { a: Int, b: Int ->
        a + b
    }

Let’s see writing and calling a lambda

Say we have function calculator, which takes lambda. So it will be called in following manner:

// Function
fun calculator(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

// Lambda
val multiplication: (Int, Int) -> Int = { a: Int, b: Int -> a * b }

// Calling the function
calculator(2, 3, multiplication)

Now this lambda can be written in different ways

// Specifying variable and lambda function type 
val multiplication: (Int, Int) -> Int = { a: Int, b: Int -> a * b }

// Without specifying variable type- when type of lambda is mentioned
val multiplication: (Int, Int) -> Int = { a, b -> a * b }

// Without specifying lambda function type- when the variable types are mentioned
val multiplication = { a: Int, b: Int -> a * b }

// More lambda signatures
val emptyLambda = { }
val emptyLambda: () -> Unit = { }

val printMessage = { print("You are amazing...") }
val printMessage: () -> Unit = { print("You are amazing...") }

Also the function calculator(…) can be called in different ways like

// Specifying the name of lambda
calculator(2, 3, multiplication)

// Writing the signature and  lambda function in place of lambda parameter
calculator(2, 3, { a: Int, b: Int -> a * b })

// Writing the lambda function outside of function call, when lambda is the last parameter of the function
calculator(2, 3) { a: Int, b: Int -> a * b }

Real world usage/applications of lambda

Lambda’s usage as callback function:

without lambda, we will create interface, implement it and call it from another calling class. But with lambda we just have to define lambda, pass it in a function and simply call it.

Let’s see the code with interface first:

class Model : APICallback {
    fun callAPI() {
        val repository = Repository()
        repository.callAPIFromRepo(this)
    }

    override fun success() {
        // API's response received successfully
    }
}

class Repository {
    fun callAPIFromRepo(APICallback: APICallback) {
        // API call here and then call API success
        APICallback.success()
    }
}

interface APICallback {
    fun success()
}

let’s see above code simplified with lambda:

class Model {
    val apiResult: (String) -> Unit = { response ->
        // Consume response here
    }

    fun callAPI() {
        val repository = Repository()
        repository.callAPIFromRepo(apiResult)
    }
}

class Repository {
    fun callAPIFromRepo(apiResponse: (String) -> Unit) {
        // API call here and then call lambda
        apiResponse("API success")
    }
}

Recommended articles

https://kotlinlang.org/docs/lambdas.html

https://developer.android.com/codelabs/basic-android-kotlin-compose-function-types-and-lambda#0

Recommended videos

Leave a Comment