<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sanjay Mallur on Medium]]></title>
        <description><![CDATA[Stories by Sanjay Mallur on Medium]]></description>
        <link>https://medium.com/@sanjaymallur?source=rss-b8e031e04146------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eIpzruOwz3SX9AROQvUqGA.png</url>
            <title>Stories by Sanjay Mallur on Medium</title>
            <link>https://medium.com/@sanjaymallur?source=rss-b8e031e04146------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 18:37:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sanjaymallur/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Diving into Higher order functions and lambdas in Kotlin]]></title>
            <link>https://sourcediving.com/diving-into-higher-order-functions-and-lambdas-in-kotlin-e07656cdffe1?source=rss-b8e031e04146------2</link>
            <guid isPermaLink="false">https://medium.com/p/e07656cdffe1</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[higher-order-function]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Sanjay Mallur]]></dc:creator>
            <pubDate>Thu, 20 Jun 2019 13:39:15 GMT</pubDate>
            <atom:updated>2019-06-22T07:00:59.225Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JwrsmIRcgM2IlCxy" /><figcaption>Photo by <a href="https://unsplash.com/@teddykelley?utm_source=medium&amp;utm_medium=referral">Teddy Kelley</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When I joined <a href="https://www.cookpadteam.com/"><em>Cookpad</em></a> few months back, I was a little muddled when first looking at the code, and no wonder why, as the codebase extensively uses <em>Lambdas</em> and <em>Higher order functions </em>and these were new to me. I had to take some time to explore and here are some findings that might come in handy for you too.</p><h3>1. Lambdas</h3><p>Let’s dive into definitions of Lambda expressions: <strong><em>Lambda expressions and anonymous functions are ‘function literals’, i.e. functions that are not declared, but passed immediately as an expression</em></strong>. By <em>“expression”</em> we mean we can pass as <strong><em>arguments</em></strong> to functions or <strong><em>return</em></strong> as a function. Let’s discuss this with example to understand more clearly:</p><pre>val multiply : <strong>(Int, Int) -&gt; Int</strong> <strong>= { x, y -&gt; x * y }</strong></pre><p>In the above code, the variable <strong><em>multiply</em></strong><em> </em>will be stored as a<em> function </em>in memory when executed. And if we look at the code at a leisurely pace:</p><p>1. <strong><em>(Int, Int)</em></strong> acts as <em>arguments</em> and<br>2. <strong><em>Int</em> </strong>after right arrow( →) acts as <em>return type</em>.<br>3. And if you look at on the other side after equals(=), we can write any logic inside <strong>{ } </strong>which is a function<strong> </strong>to return Integer value from our logic.</p><p>We can also write above function like this:</p><pre>val multiply <strong>= { x:Int, y:Int -&gt; x * y }</strong></pre><p>Where kotlin type inference will know the return type, which is <strong><em>Int</em></strong>. So considering our example, we can simply call <em>multiply</em> function with two <em>Integer</em> arguments:</p><pre>val result = multiply(3 , 6) <strong>//returns 18</strong></pre><p>I hope lambdas are bit clearer now, moving on to:</p><h3>2. Higher order functions with lambdas</h3><p>As per the definition from wikipedia: <strong><em>A higher order function is a function that takes functions as parameters, or returns a function</em></strong>. Let’s discuss this with an example and main structure like below:</p><pre>class MainClass {</pre><pre>               fun function1(function2 : () → Unit) {</pre><pre>                   function2() //<strong>Executes function2 here</strong></pre><pre>               }</pre><pre>}</pre><p>Let’s split this function into different parts to understand it better:</p><p>1. <strong>function2</strong> is name of the function<br>2. <strong>()</strong> represents that function2 takes no arguments<br>3. <strong>Unit</strong> represents function2 does not return anything.</p><p>If we combine altogether: <br>i. <strong>function1</strong> is a function which takes <strong>function2</strong> as an argument<br>ii. <strong>function2</strong> is a function which takes zero parameters and does not return anything</p><p>You might be confused by reading word “<em>function</em>” over and again but I recommend you to read at leisurely pace to understand it clearly.</p><p>Now it’s time to understand these functions with example in Android:</p><pre>fun Group.setAllOnClickListener(listener: () -&gt; Unit) {</pre><pre>    <em>referencedIds</em>.<em>forEach </em><strong>{ </strong>id <strong>-&gt;</strong></pre><pre><strong>        </strong><em>rootView</em>.findViewById&lt;View&gt;(id).setOnClickListener <strong>{      </strong></pre><pre>        listener() <strong>// listener() function executes here</strong></pre><pre>        <strong>}<br>    }<br></strong>}</pre><p>Again, let’s split this function into different parts to understand it better:</p><p>1. <strong>Group </strong>is widget from constraintLayout<br>2. <strong>setAllOnClickListener</strong> is extension function which takes <strong>listener: () -&gt; Unit </strong>as<strong> </strong><em>argument</em><strong> </strong>which does not return anything.</p><pre>class ExampleViewHolder {<br><br>    fun bind(user: User, onUserGroup: (userId: String) -&gt; Unit) {<br><br>        openUserDetails.<em>setAllOnClickListener </em><strong>{<br>            </strong>onUserGroup(user.id)<br>        <strong>}<br></strong><br>    }<br>}</pre><p>In above code :</p><p>1. <strong>ExampleViewHolder </strong>is a view holder class in recycler view<br>2. <strong>bind</strong> is a function to bind item in recycler which takes <strong><em>onUserGroup: (userId: String) -&gt; Unit </em></strong>as <em>argument</em><strong><em><br></em></strong>3. <strong>openUserDetails </strong>is a group view which is holding layout references inside <em>ConstraintLayout</em></p><p>The above code is small example to understand how we can use <em>Higher order functions</em> seamlessly in Android. There are many other ways how you can take advantage of Higher order functions in Kotlin, but for this small post I would like to keep it simple with above example.</p><p>Yayy!! that’s the end of this <em>3 min read </em>post, I hope you have learnt something interesting. If you like it give thumbs up and share with your <em>Kotlin buddies.</em></p><p>At <a href="https://www.cookpadteam.com/"><em>Cookpad</em></a> our Android codebase is entirely cooked with Kotlin and we always thrive to utilise Kotlins best features. Thanks to all my teammates for such nice job :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e07656cdffe1" width="1" height="1" alt=""><hr><p><a href="https://sourcediving.com/diving-into-higher-order-functions-and-lambdas-in-kotlin-e07656cdffe1">Diving into Higher order functions and lambdas in Kotlin</a> was originally published in <a href="https://sourcediving.com/">Source Diving</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>