Lambda functions
Siddhant Mishra
Senior Android Developer at Teachmint | Kotlin and Java | DSA - 300+ on Leetcode | I read and write about Android and DSA
Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally higher order functions we use in our aynschoronous programming for delegating some tasks to the callbacks when we recieve a certain result from the server/db use lambda functions for better readability.
But we generally fail to distinguish the following concepts behind it -
1) Lambdas taking the object as a parameter
2) Lambda taking the object as a receiver
In the above image, function manipulate1 takes in operation as a lambda function which in turn takes an integer as a parameter and returns an integer.
Whereas function manipulate2 takes in operation as a lambda function which inturn is a function literal that takes an integer as its receiver and applies the transformation on it.
Let us see the usage of the two -
By using the second method(i.e manipulate2), we reduce the syntax we need to write for achieving the same thing. And let us finally check the results -
I hope you learnt something from this article, in case you did do follow me for more such content on Android, DSA and System Design.
Thanks for reading.
Android Developer | 3+ Years' Experience | Proficient in Jetpack Compose, App Security & Performance
4 个月Interesting