Syntax of the Lambda function or expression in Kotlin:
The general syntax of the lambda function or expression we used in Kotlin is given below:
We have to initialize a lambda function or expression in a variable mentioned in the syntax. The curly braces surround the lambda expression, and argument declarations are also inside the curly braces. Argument declarations go inside curly braces. The inclusion of optional annotations and code body comes after the arrow sign “->.” The lambda body code cannot be empty. If there is no argument on the left, then the arrow sign “->” can be removed. The return statement is considered lambda’s last expression. The unit type will be utilized as a user-defined function if no value is provided or the return type is not specified.
How to utilize Lambda function in Kotlin:
To understand the basics of using the lambda function or expression in Kotlin language, we should take a look at the examples given below:
Example # 1: Using Lambda invoke a function in Kotlin:
We can create a kotlin expression that doesn’t take any parameters by calling invoke() function. Lambda expression functions are invoked in the code implementation.
The main function has passed with the command line argument as Array[String]. Then, we have created a variable with the keyword “val” inside the main function. The variable is defined with the name “welcome.” The variable “welcome” is subsequently allocated to the lambda expression. At last, we have invoked a function by calling welcome.invoke() in the code.
The output of the lambda invokes method is shown on the console screen.
Example # 2: Using “in” type inference in lambda function in Kotlin:
The type inference in kotlin plays a significant role as it enables the compiler to determine the type of lambda expression. Here, we have computed the product of the two integers by using the lambda expression shown below.
We have a main function declared using the keyword “fun” in the above code. The main function will help us execute the lambda expression’s code implementation. Firstly, we have created a variable as “val” and defined the val as “myProduct.” The variable “myproduct” is taking the lambda expression. Then, we have converted the integer to string with the help of the toString() function, which is a built-in kotlin function. After converting the integer to string, we have a new variable declaration as “myresult.” In the variable “myresult,” we have passed the values on which product operation is done. In the end, the kotlin println function will return the string from the integer value.
The product of two numbers is printed on the terminal screen by using the lambda function of type inference.
Example # 3: Using type declaration in Lambda function in Kotlin:
The type of our lambda expression must be declared explicitly. If lambda doesn’t return a value, we can use a unit indicated with the “unit” keyword in the code.
In the code, we have a declaration of integer and string to a variable “X1” and “X2”. The variable “X1” has a lambda expression in which product operation is done. The variable “X2”’ having string concatenation operation passed as a lambda expression. That means the lambda expression will return the value of the corresponding type declared. Then, we have a “unit” keyword in a variable “X3,” which suggests that lambda returns no value. In the main function, we have printed the values of the variables.
Upon execution, we have the output in the image below.
Example # 4: Using “in” Lambda function as a class extension in Kotlin:
The lambda expression can also be used as a class extension. We can pass the parameter: “it” and “this” in lambda expressions. The keyword “this” is used for the string, and the “it” keyword is used for the int parameter in lambda. These keywords will pass in the lambda expression, and we can then concatenate the values and assign them to the result variable.
In the above code, we have a lambda expression that acts as a class extension and assigns this expression to a variable “myStr1”. The variable “myStr1” has type declared as “String. (Int)->String”. The lambda expression takes the “this” keyword for string and the “it” keyword for the integer value. The “+” operator is used for concatenation. We have added a new variable called “strResult” to the main function. The variable “strResult” is initialized with the string and integer values. Through the println() function, we will print the output on the console screen.
The resultant string from the above execution of code is displayed below.
In most cases, these lambda functions typically have only one parameter. The “it” keyword indicates a single parameter passed into the lambda expression. The “it” keyword is the implicit name of a single argument. Below, we have a shorthand form of code that provides a short way to assign an expression to a variable.
In the above code, in the main function, we have called a variable “myArray” along with the filter method. The filter operation reads the data and helps in transforming the data according to the given condition. Then, we passed the lambda expression in the myArray.filter function. The lambda function uses the “it” keyword with the given condition. The condition given is that the “it” keyword should be greater than 5. The println method will print the new data on the console screen below.
We have an output of positive integer values from the given condition as these integers are greater than 5.
The main aim of the article is to demonstrate to you the concept of lambda function and expression in the kotlin language. The lambda function has concise code, which will save you time. To better understand, we have different cases of using lambda functions in the examples. It will help you understand the logic we have implemented for the lambda function in kotlin.