Lambda functions

Lambda functions

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

Object as param and object as 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 -

Usage

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 -

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.


Aman Rawat

Android Developer | 3+ Years' Experience | Proficient in Jetpack Compose, App Security & Performance

4 个月

Interesting

要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • SuspendCoroutine

    SuspendCoroutine

    Today I will be writing about the usage of an important concept in Android namely suspendCoroutine. By definition…

  • findViewById - Internals

    findViewById - Internals

    Hope everyone is doing well. Today I will be writing about the internal working of findViewById in Android.

    2 条评论
  • Exceptions in Coroutines

    Exceptions in Coroutines

    Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully. First let us see…

  • Trie data structure

    Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • Vertical Order traversal in binary tree

    Vertical Order traversal in binary tree

    We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not…

  • LCA in Binary Tree

    LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

  • Reified in Kotlin

    Reified in Kotlin

    Today I will be writing about the type erasure in Java and Kotlin and its solution. The concept of type erasure stems…

    1 条评论
  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论

社区洞察

其他会员也浏览了