In Kotlin there is a function called Lambda, which is also called an anonymous function. A Lambda function is a short and simple way to define a function without an explicit name.

A lambda function is written in parentheses and has a single expression that is used as the return value. Here is a simple example of a Lambda function in Kotlin:

A lambda expression can be stored in a normal variable and then called like a regular function using the name of that variable.

In this example we define a Lambda function {println(“Hello Kotlin!”)} and store it in the variable greeting. We can then call the Lambda function by specifying the name of the variable greeting and an empty argument list () use. In this example we call the function three times and all three times will 'Hello Kotlin!' issued. Since a lambda expression represents a shortened form of a function, the variable greeting the function type () -> Unit.

A lambda expression can also be executed like a normal function by using parentheses.

Passing parameters to Lambda

Lambdas, like functions, can take parameters. To pass parameters to a lambda expression, the arrow -> used. The parameters are specified to the left of the arrow, and the body of the lambda expression, i.e. the code to be executed, is to the right of the arrow.

A lambda expression can be called directly when it is defined by specifying the parameter values ​​in parentheses after the expression. This is useful when you only want to execute a function once and don't need a separate variable to store the function.

If a lambda expression has multiple parameters, they are separated by commas to the left of the arrow:

Here is an example in Kotlin that demonstrates a lambda expression with multiple parameters:

If you want to execute multiple statements in a lambda expression, these statements can be placed on separate lines following the arrow. Below is an example that demonstrates a lambda expression with multiple actions:

Lambda expressions as arguments in functions

Lambda expressions can be used as arguments to functions if they represent the same type of function. Here is our own example in Kotlin showing this use of lambda expressions:

This example defines two lambda expressions: multiply and divides. Both lambda expressions take two integer parameters and return an integer value. The function performOperation also has two integer parameters x and y and another parameter operation, which represents a function that takes two integer parameters and returns an integer value.

Within the performOperationfunction, the passed operation (multiply or divides) with the parameters x and y called and the result is printed on the console.

In the main function will performOperation called twice, once with the multiply-Lambda and once with the divides-Lambda.

Typing of lambda parameters

When a lambda is assigned to a parameter or variable with an explicitly specified type, the parameters' types can be omitted from the lambda expression. Here is our own example in Kotlin showing type inference for lambda parameters:

In this example the lambda expression subtract a variable with the explicitly specified type (Int, Int) -> Int assigned. Since Kotlin recognizes that both the first and second parameters are of type Int the types of parameters in the lambda expression can be omitted: { x, y -> x – y }. Kotlin automatically understands the parameters x and y of the type Int .

The same applies to calling the function performOperation(). When passing the lambda to this function, Kotlin automatically detects the type of parameters. In the example the function performOperation with the values 9 and 6 and a lambda expression that multiplies the two parameters.

trailing lambda

In Kotlin the term refers trailing lambda refers to a syntax convention in which a lambda expression that serves as an argument to a function is written after the function's parameter list rather than within the parentheses. This syntax is used when the function parameter that the lambda accepts is the last in the parameter list.

The use of a trailing lambda can make the code more readable and cleaner, especially when the lambda expression contains multiple lines or more complex logic. An example of the use of trailing lambda in Kotlin:

In this example the function has performOperation a parameter operation, which is a lambda expression. Since this parameter is the last in the parameter list, the lambda expression can be written after the parameter list instead of in the parentheses:

Instead of the traditional spelling:

Lambda expression as a return value from a function

A function can also return a lambda expression as a return value that corresponds to the return type of the function. Here is an example in Kotlin that shows how a function can return a lambda expression:

In this example we have a function createAdder, which is an integer parameter x accepts and returns a lambda expression, which in turn accepts an integer parameter and returns an integer value. The return type of the function is (Int) -> Int.

Inside the function createAdder will return a lambda expression that contains the value x with a different integer value y added. Because the lambda matches the expected return type of the function, it can be used as the return value.

At the nuclear mainfunction we call createAdder with the value 5 and have the returned lambda of the variable viper to. Then we call viper with the value 3 on and give the result 8 on the console.

Unused parameters

In some cases, a lambda expression may contain parameters that are not used in the body of the lambda expression. In such situations, Kotlin can replace unused parameters with underscores _ be replaced to make the code cleaner and easier to understand. This example shows how unused parameters in lambda expressions in Kotlin can be replaced with underscores to make the code cleaner and easier to understand.

In this example we have a function selectAction, which accepts an integer parameter number and returns a lambda expression that accepts two integer parameters and returns an integer value. The return type of the function is (Int, Int) -> Int.

Inside the function selectAction Let's use a when statement to return different lambda expressions based on the value of number. If number neither 1 or 2 is, it returns a lambda expression that simply contains the value 0 returns. Since the parameters are not used in this lambda expression, they can be replaced with underscores:

In the main function we call selectAction with the value 4 and have the returned lambda of the variable action to. Then we call action with the values 5 and 3 on and give the result 0 on the console.

What are the benefits of lambda expressions?

Lambda expressions offer the following advantages:

  1. Shorter and clearer syntax: Lambdas enable a compact and expressive way of writing functions, especially short and simple functions. They allow functions to be defined as expressions, making the code shorter and easier to read.

  2. Anonymous features: Lambda expressions are anonymous functions, meaning they have no name. This is useful when a function is only needed at a specific location in the code and is not reused. Instead of creating a separate, named function, a lambda expression can be defined and used directly where needed.

  3. Functional programming: Lambda expressions support functional programming concepts such as higher order functions and working with collections. They allow functions to be passed as parameters to other functions or to use functions as return values. In functional programming languages ​​or when using functional programming paradigms, lambda expressions are a fundamental tool.

  4. Easier manipulation of data structures: Lambda expressions are often used in conjunction with collections and data structures to simplify operations such as filtering, transforming, or aggregating data. Lambdas allow such operations to be expressed in a simple and intuitive way. Here is a simple code example for Collection List:

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked