![]() |
VOOZH | about |
In this article, we are going to learn lambdas expression and anonymous function in Kotlin. While syntactically similar, Kotlin and Java lambdas have very different features. Lambdas expression and Anonymous function both are function literals means these functions are not declared but passed immediately as an expression.
Table of Content
As we know, the syntax of Kotlin lambdas is similar to Java Lambdas. A function without a name is called an anonymous function. For a lambda expression, we can say that it is an anonymous function.
val lambda_name : Data_type = { argument_List -> code_body }A lambda expression is always surrounded by curly braces, argument declarations go inside curly braces and have optional type annotations, the code_body goes after an arrow -> sign. If the inferred return type of the lambda is not Unit, then the last expression inside the lambda body is treated as return value.
val sum = {a: Int , b: Int -> a + b}In Kotlin, the lambda expression contains an optional part except for the code_body. Below is the lambda expression after eliminating the optional part.
val sum:(Int,Int) -> Int = { a, b -> a + b}Note: We don't always require a variable because it can be passed directly as an argument to a function.
Output:
GeeksforGeeks
GeeksforGeeks
Output:
The sum of two numbers is: 5
The sum of two numbers is: 7
12
Kotlin’s type inference helps the compiler evaluate the type of a lambda expression. Below is the lambda expression using which we can compute the sum of two integers.
val sum = {a: Int , b: Int -> a + b}
Here, Kotlin compiler self evaluate it as a function which take two parameters of type Int and returns Int value.
(Int,Int) -> Int
If we wanted to return String value than we can do it with help of toString() inbuilt function.
Output:
The sum of two numbers is: 5
In above program, Kotlin compiler self evaluate it as a function which takes two integer values and returns String.
(Int,Int) -> String
We must explicitly declare the type of our lambda expression. If lambda returns no value, then we can use: Unit.
Pattern: (Input) -> Output
val lambda1: (Int) -> Int = {a -> a * a}
val lambda2: (String,String) -> String = { a , b -> a + b }
val lambda3: (Int)-> Unit = {print(Int)}
val lambda4: String.(Int) -> String = {this + it} Here, it represents the implicit name of the single parameter, and we will discuss it later.
Output:
Geeks50In the above example, we are using the lambda expression as class extension. We have passed the parameters according to the format given above. this keyword is used for the string and it keyword is used for the Int parameter passed in the lambda. Then the code_body concatenates both the values and returns to variable result.
In most of cases lambdas contains the single parameter. Here, it is used to represent the single parameter we pass to lambda expression.
Output:
[1, 3, 5][1, 3, 5] After executing a lambda function, the final value returned can be any of the following: Integer, String, or Boolean.
Output:
Number is even and positiveAn anonymous function is very similar to a regular function except for the name of the function, which is omitted from the declaration. The body of the anonymous function can be either an expression or a block.
fun(a: Int, b: Int) : Int = a * bfun(a: Int, b: Int): Int {
val mul = a * b
return mul
}
Output:
The sum of two numbers is: 8
The multiply of two numbers is: 15
The only difference is the behavior of non-local returns. A return statement without a label always returns from the function declared with the fun keyword. This means that a return inside a lambda expression will return from the enclosing function, whereas a return inside an anonymous function will return from the anonymous function itself.