Learning Kotlin 2️⃣: Lambda Function
Tags: kotlin, programming, todayisearched, android
Published: 2021-04-27 02:04:31

<p>Welcome to part 2 of the learning Kotlin series! If you haven’t yet, check out my<a href="https://josephamaurer.medium.com/learning-kotlin-programming-with-visual-studio-code-a69e4daa0348"> first post</a> that goes over getting started with Kotlin. In this post, we will be going over Lambda expressions and the syntax you can expect to see when using them. Let’s dive right&nbsp;in!</p>
<p>In case you are unfamiliar with lambda expressions, lambda expressions are a shorter way to define a function and its implementation. They are incredibly useful because you can store them in variables and execute them as regular functions. Because they can be defined as variables, you can also pass them as parameters to other functions or even be returned from a function.</p>
<p>You can define a lambda expression with the following syntax:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/710/0*8YPlDPkF4q6lgRK9"><figcaption>Example 1: Simple Lambda&nbsp;Multiply</figcaption></figure><p>In this snippet, we are defining a variable called ‘multiplyLambda’ that is a lambda function. The function takes two parameters, x and y which are both integers. What’s interesting about this lambda is that the value from the last expression is implicitly returned. You can explicitly return a value using the <a href="https://kotlinlang.org/docs/returns.html#return-at-labels">qualified return syntax</a> if you aren’t a fan of the auto&nbsp;return.</p>
<p>Kotlin’s <a href="https://kotlinlang.org/spec/type-inference.html">type inference</a> is very similar to c# here where the type is determined at compile time and therefore can be omitted from the declaration. In Kotlin, there are two types of inference that are supported.</p>
<ul>
<li>Local type inference: which is for locally defined statements.</li>
<li>Function signature type inference: is what was talked about above where the inferred type of a function return value is calculated.</li>
</ul>
<p>The example above is a simplified example of a lambda function. Here is a breakdown of all of the possible parts of&nbsp;one.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lv2Vg3IOwTWZulk0"><figcaption>Example 2: Lambda function components. Source: <a href="https://www.raywenderlich.com/2268700-introduction-to-kotlin-lambdas-getting-started">Raywenderlich</a></figcaption></figure><p>The important things to remember&nbsp;are:</p>
<ul>
<li>A lambda expression is always surrounded by curly&nbsp;braces.</li>
<li>Parameter declarations (i.e. param1: Int) in the full syntax go inside of the curly braces and have those optional type annotations.</li>
<li>The body is always after the →&nbsp;sign</li>
<li>If the inferred return type of the lambda expression is not a Unit, the last expression inside the lambda body is treated as the return value (i.e example 1 the return is&nbsp;x*y)</li>
</ul>
<h3>Use that&nbsp;Lambda</h3>
<p>As mentioned earlier, you can use lambda functions in a variety of ways. I know in c#, a common use case is using lambda expressions for event handling; i.e. button presses, event handlers, etc. According to this <a href="https://www.raywenderlich.com/2268700-introduction-to-kotlin-lambdas-getting-started">post</a>, that is also a common use with Kotlin when making an app. For our example, once we define the lambda expression, using it is relatively easy.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/516/0*q37ZPIdJWcXismwx"><figcaption>Example 3: This would call the lambda we made in example 1 and print the&nbsp;output.</figcaption></figure><p>If the lambda only has one parameter and the compiler can figure out the signature itself, then you are allowed to not declare the parameter and use ‘it’ instead. Let’s consider the following example:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vvQrvZeA_yVOROe-"></figure><p>The benefit of this type of syntax is that you can do something very similar to c# LINQ-style code:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U2xIXP94hlHeNt4s"></figure><p>I hope this breakdown helps you not be confused or intimidated by lambda functions going forward. At the end of the day they are just fancy functions.</p>
<p><strong>If you have questions or comments, please comment below! Happy&nbsp;Coding!</strong></p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=dc9fa041fcf9" width="1" height="1" alt="">
</div>