Understand Lambdas and High Order Functions with Multiple Variations
Hey Guys, I am back with amazing topic Lambdas and High Order Functions which actually is the advantage of Kotlin over Java
When I started learning Kotlin, ,most Different & bit Difficult concept I found was the Lambdas and High order functions, I googled a lot to learn these two things, and guess what? I found some different way everywhere explaining these two concepts and no where I got everything at a single Place
Hence I’ll not only make you understand these two concepts but also give you Many More Variations of Lambda & High Order Function 🤘
Lambda Functions
- Anonymous functions (function without Name)
- PURPOSE :- Can be treated as a value for example (Pass them as arguments to functions, return them, or do any other thing we could do with a normal object)
- Syntax — Starts with { and ends with } and anything under that block — has no name of that function
- Example — { println(”hey puneet I am lamba? “) }
- Now above lambda takes no param and returns nothing (no value)
- But there could be a scene when we want to send some param and return some param
Now, let’s learn it step by step with a simple example in Kotlin:
1. Lambda With No Arguments and No Return Value
2. Lambda with Return Value and No Arguments
3. Lambda with One Argument and No Return Value
4. Lambda with Both Argument and Return Value
5. Lambda with Multiple Arguments and Return type
You might observed in above code that if any argument is not in use then It can be replaced with underscore ‘_’ as Kotlin is very concise
-> Now Let’s learn how to Call Lambda functions
So this is how we can call lambda functions passing proper arguments while calling them
Important Point ->
Let’s say we have a normal function with same name and signature as lambda functions, So whenever we will call that function It will give Preference to Normal Functions instead of Lambda functions
Now What's the Benefit of lambda if same can be done with functions?Lambdas are treated as Objects which can be passed as argument and can be returned as a valueHere comes High order function into Picture
High Order Functions
A higher-order function is a function that takes functions as parameters or returns a function.
> How High Order functions are Linked with lambda?
Instead of Integer, String or Array as a parameter to function, we will pass anonymous function or lambdas
We can pass a lambda expression as a parameter to Higher-Order Function
Example:-
•This takes a lambda function printNow: () -> Unit
•printNow is just the name for the parameter. It can be anything. We just need to use this when we execute the function.
•() -> Unit, this is important.
•() represents that the function takes no parameters.
•Unit represents that the function does not return anything.
•So, the passLambda can take a function that takes zero parameters and does not return anything.
-> As Kotlin is a very concise language, there is an other way to pass Lambdas in High order functions omitting brackets
-> We can pass directly function also in High order function like this
Important Point
Now Let’s discuss this point in relation with the Important Point discussed above in Lamda function
Let’s say we have a high order function which takes one string and one lambda function as an argument
This is how we can call this function ->
Here while calling high order function, we have passed lambda function lambdaWithMultipleArguments1 which we had created above i.e It takes two params and return addition of both
We can call it again passing different lambda of Subtraction definition
Hence It Provides us the Flexibility!
Yes, So first of all while passing arguments it takes Lambdas as a priority not a normal function reason being normal functions can’t be passed as an argument in a functions
and Secondly It provides us the flexibility to call any argument which in turn doing some other operation So Isn’t it Cool?
High Order Function Example in Android
•If we want to click on some item in list/Rv, we create an interface for that, set interface in adapter, call interface function and override that interface in UI- frag/activity
•So ultimately, we write a lot of code to accomplish this task
•This can be done very easily with High order functions / lambdas and that too without boilerplate code
Fragment ->
Initialisation of Adapter->
messageAdapter = MessageAdapter(onMessageItemClick)
Adapter->
Calling High order function from Adapter on clicking of an Item->
This is the Beauty of High order functions and Lambdas
That’s it from my side!!
Share this blog to spread the Knowledge | Do Follow and CLAP if you Liked it
Happy Coding!!
Add me on Linkedin and Twitter for more exciting updates and concepts