Kotlin Lambda Function

Lambda is a function which has no name. It is a concise way to define a block of code that can be passed around as an argument to higher-order functions or stored in a variable for later use.

Lambda functions are useful in various contexts, such as higher-order functions, collections operations (like map, filter, and reduce), and asynchronous programming.

The general syntax for a lambda function in Kotlin is as follows:

val lambdaName: (parameter1Type, parameter2Type, ...) -> ReturnType = { parameter1, parameter2, ... ->
    // Function body
    // Return value (if applicable)

1. lambdaName: This is an optional name for the lambda function, which can be used if need to reference the function elsewhere.

2. (parameter1Type, parameter2Type, …): These are the parameter types that the lambda function takes. Lambda functions can have zero or more parameters.

3. ReturnType: This is the type of the value that the lambda function will return. you can use Unit If the function doesn’t return anything.

4. ->: This is the lambda operator, which separates the parameter list from the function body.

5. Function body : This is the code that makes up the body of the lambda function. It gets executed when the lambda is called.

Now, let’s see some examples of lambda functions:

Example 1: A lambda function with parameters that adds two numbers and returns the result:

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

val result = addNumbers(5, 3)
println("Result: $result")  // Output: Result: 8

Example 2: A simple lambda without parameters:

val greet: () -> Unit = {
    println("Hello, World!")
// Calling the lambda function 
greet() // Output: Hello, World!

Example 3: Lambda as an argument to a higher-order function:

// Higher-order function to perform a mathematical operation on two numbers
fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)

fun main() {
    // Using the 'calculate' function with different operations using lambdas
    val addResult = calculate(6, 8) { x, y -> x + y }
    println("Addition Result: $addResult") // Output: Addition Result: 14

    val subtractResult = calculate(10, 7) { x, y -> x - y }
    println("Subtraction Result: $subtractResult") // Output: Subtraction Result: 3

    val multiplyResult = calculate(14, 6) { x, y -> x * y }
    println("Multiplication Result: $multiplyResult") // Output: Multiplication Result: 84

In this example, calculate is  a higher-order function that takes two integers a and b, and a lambda operation that takes two integers and returns an integer. The calculate function applies the lambda operation to the input integers a and b and returns the result.

Example 4: return a lambda function from another function

This feature allows you to create functions that generate and customize behavior on-the-fly based on certain conditions or inputs.

Here’s an example:

// Function that returns a lambda
fun createAdder(amount: Int): (Int) -> Int {
    return { x -> x + amount }

fun main() {
    // Create an adder function that adds 6 to any input
    val addSix = createAdder(6)

    // Use the returned lambda to perform addition
    val result1 = addSix (10) // 10 + 6 = 16
    val result2 = addSix (20) // 20 + 6 = 26

    println("Result 1: $result1") // Output: Result 1: 16
    println("Result 2: $result2") // Output: Result 2: 26

In the main function, we call createAdder(6) to get the addFive lambda. then use addFive to perform addition on different input values (10 and 20 in this case) As a result, we get 16 and 26, respectively, because the addFive lambda adds 6 to the given input..