Simplifying Lambda Expression and Higher-Order Function in Kotlin
Let’s get you started to easily learn Lambda Expressions and HOFs
Lots of beginners are facing problem to understand the Lambda Expression & Higher Order Function and both are very important if you want to write more readable, maintainable and concise code in your program. You must use lambda expression for function or method in your program wherever required. So we will see one-by-one, what is lambda expression followed by higher-order function.
What is Lambda Expression?
Lambda expression is a function without name, modifier and also without fun key of kotlin function
Lets first see how lambda expression looks:
As you can see above differences between lambda expression and normal function, we just removed the method name & fun key from normal function.
- Left-Side of the arrow(->) you define your function’s parameters.
- arrow (->) it just a part of the syntax of lambda expression and separate the parameters & function’s body like (params)->{statements}.
- Right-Side of the arrow(->) your function’s body as println(data).
- You can call this test variable as function like test(“hello”).
It is just overview now we will see different types of lambda expression declarations.
val test1 : ()->Unit = { println("Hello") }//as you can see we are declaring val, check the formatval name : Type = value
or
val test1 = { println("Hello") }
- Above lambda expression is taking no params & return nothing, as you see above we have two types declarations of lambda and both are right, you can use anyone. ()-parenthesis used for params and it is empty, meaning there is no parameter given in this function. Unit-it is the same as the void in java meaning when there is no return type just use this.
val test2: (Int)->Unit = { println(it+2) }
or
val test2 = { a:Int-> println(a+2) }
2. Above test2 lambda expression is taking one integer as param and returns nothing that’s why we use Unit as return type. I know you are worried about what is that “it”? don’t worry it is just a param that we have given, so if lambda expression contains only one param you can infer by it.
NOTE: By these following expressions you just make sure that what are the parameters and return type of any lambda expression going to use, so the syntax is (params)->return.
1.(Int)->Unit
2. (Int)->Int
3. (Int, Int)->Int
4. etc……
val test3: (Int)->Int = { a:Int->a*a }
or
val test3 = { a:Int->a*a }
3. As you can see the above test3 expression takes an Int param and returns Int.
4. One last type of declaration for you to have more understanding of these expressions.
As you can see below test4, we have not declared the type of a & b but it is still valid because the compiler knows by syntax (Int, Int)->int and there is also a return which is the multiplication of a and b.
val test4: (Int,Int)->Int = { a,b->a*b }
So now you have very good understanding what lambda expression is. As I told you that we will also learn about the Higher Order Function
What is Higher Order Function?
A function which follow the any given condition are known as Higher Order Function:-
1. takes function as parameter
2. returns function
3. or can both
→ let’s see how we can give function in the parameter of another function
fun funName(func: (Int) -> Int) {
}
as you can see above that I have given a lambda expression as a parameter in function funName which is func, you can give any name as I am using func because it is a name and it takes an integer param and also return an integer, don’t worry we will also see how to use it later in this blog but first let me make you familiar with HOF(Higher Order Function).
→Now see a function which returns a function
fun retunAfunc(): (Int) -> Int {
}
In the above function, we return a function which takes an integer param and returns an int after multiplying by 2
→last one, a function which takes a function as a parameter and also returns a function
fun func(test:(Int)->Int):(Int)->Int{
}
the above function takes a function as the parameter which takes an integer as a parameter and returns an integer.
And above function also return a function that takes an integer as a parameter and returns an integer.
So you don’t need to worry about how we use this type of function just walk with me step-by-step.
Now we see how to use all three types of HOF one-by-one then you will have a better understanding of it.
- Function takes another function as a parameter
above function takes two parameters one is an integer and another is a function.
So we have two options for calling this higher-order function
- In the first type, we declare parameter function in a variable give it to the funName, see below
Note: (params)->return , this syntax should be match with the function as we doing -> val test:(Int)->Int = { it*2 } with parameter function.
val test:(Int)->Int = { it*2 }
val a:Int = 3funName(a,test)// it will print 6
- In the second type, we define the parameter function in the function funName while calling and at the time of calling we also define what kind of operation the parameter function will do with its parameters.
val a:Int = 5funName(a, { it*2 })// it will print 10
As you can see above you don’t need to declare parameter function in a variable, just write your code between curly braces as above.
If the parameter function contains two or more parameter you can do just like this, see below.
You can add any number of parameters in function funName() and parameter function as per your requirement.
2. Function returns a function
Above HOF returns a function that takes an integer parameter and returns an integer.
When we call returnAfunc(3) it returns a function in which we multiplying integer parameter of HOF (a: Int) with returned function’s parameter (x: Int) and the result you get is 6 in the above case.
3. Function takes a function as a parameter and also return a function
So this HOF function doing both take a function as a parameter and return a function.
It looks a little bit clumsy, Right?….
But wait it is as simple that we have seen before, please go through the following points below.
- In HOF we have given a value 6 to integer parameter of parameter function
- val test= funNameTwo({it*4}) this line means 6 multiply by 4 and return 24 as you can see above and HOF also return a function as we have defined
- val s = test(7), this line multiplying 7 with the returned value of parameter function which is 24 as we are multiplying the returned value of parameter function with the parameter of returned function
- finally, you got the result = 168
IMPORTANT:- You have seen different types of higher-order functions in this article so I want you to know one thing which is very important “while calling the higher-order function we just define what kind of operation the parameter function will do”.
Conclusion
Hopefully, you enjoyed this article and now you have a better understanding of different kinds of lambda expressions and higher-order functions and how to use them in your code.
I have written my perception of lambda expression & higher-order function that I gained from doing multiple projects in the last 2 years.
Please share your opinion on what you feel about this article or if you found something wrong or anything else that you wanna share, this will help me and others reading this article.