Lambda function is powerful feature in any Programming language and Lambda function in Kotlin is no exception. In this article, we will look at how to create Lambda function in Kotlin along with simple examples.
By the end of this article, you will be able to learn:
- What is a Lambda Function
- How to Create a Lambda function in Kotlin
- Practical Examples where Lambda Function are used
Let us quickly start on what is a Lambda function.
What is a Lambda Function?
A Lambda Function is an Anonymous function which we can use similar to variables, i.e., we can either pass them as arguments to Functions or we can return the lambdas.
The functions which accepts Lambdas are called Higher order functions, which we have discussed in our previous article. If you haven’t already gone through, I would highly recommend going through this article.
How to Create a Lambda Function in Kotlin
Before creating a Lambda function, let us first see its syntax example.
In the above image, we can see that in left side of arrow, arguments are written and in right side, function block is declared. Curly braces are mandatory to write a lambda function in Kotlin.
Now to learn how to write a Lambda function, let us take a normal function in Kotin and there after modify it.
Here is a typical normal kotlin Function:
Now, if we convert the same function into a Lambda, the result would be:
We can clearly see that using a lambda turned the above function into a single liner.
Let us now declare the above function as a Lambda Expression
In the above gist, we declared a lambda function and assigned it to a variable. Now we can call the above lambda expression and pass the arguments as shown below:
We can also declare the lambda expression as shown below which is slightly different from the above example.
From the above gist, we can see that we have simplified the right part by removing the data types declared. The type of those lambda’s arguments are inferred from the variable type which we have declared in the left part.
This simplified expression can be called similar to the above example as shown in below
Now let us see few practical examples of how a Lambda function in Kotlin is used in Higher Order Functions.
Practical Examples of Lambda Function in Kotlin
Let us create a list and use forEach() function available in Kotlin’s Collection class.
forEach() function accepts a Lambda as an Argument which we have used to print each of the list item in line 3. Here “it” refers to iterator which gives us each value of list item in each iteration.
In Kotlin, we can omit parenthesis if a function has only a single lambda argument.
Now let’s use the fold() function available in Kotlin Collection class.
Here in fold function, the second argument accepts Lambda Function. The output of the above is 945.
In Kotlin, if the function’s last argument is a Lambda Function, we can write the Lambda Function outside of function. Hence the above function can be written as follows:
The output of the above is also same, i.e., 945.
With this examples, I would be concluding this article. In the next article, let us see closures in Kotlin and their comparision with Java. Comment below to let us know the feedback / queries.
Reference article: kotlinlang