Sling Academy
Home/Kotlin/Kotlin - What Are Lambda Expressions? A Beginner's Guide

Kotlin - What Are Lambda Expressions? A Beginner's Guide

Last updated: December 05, 2024

Kotlin, a statically typed programming language for JVM, Android, and the browser, introduces several features to simplify and enhance the experience of coding. Among these are lambda expressions, a concise way to represent anonymous functions. If you're a beginner, understanding lambda expressions will open up a new paradigm in programming where functions are treated as first-class citizens.

Understanding Lambda Expressions

At its core, a lambda expression in Kotlin is a function which has no name and can be passed around as a value. Lambdas are often used for operations on collections, passing behaviors, and simplifying APIs, especially in the Kotlin standard library.

Let's look at the basic structure of a lambda expression in Kotlin:

val sum = { a: Int, b: Int -> a + b }

Here, sum is a lambda expression with two parameters, a and b, both of type Int, and it returns their sum. Notice how clean and concise this looks compared to a regular function.

Lambda with Collections

Kotlin's standard library makes extensive use of lambda expressions for operations on collections. For instance, let's say you have a list of integers and want to filter out the even numbers:

val numbers = listOf(1, 2, 3, 4, 5, 6)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // Output: [2, 4, 6]

In this example, filter is a higher-order function that takes a predicate (another function) as a parameter, where it refers to the current item in the iteration.

Lambda Type Inference

Kotlin's powerful type inference means that in many situations, you don’t need to specify the types of your lambda's parameters. Here's the example of the sum lambda with type inference:

val sum = { a, b -> a + b }
println(sum(5, 3)) // Output: 8

Kotlin automatically identifies that a and b are Int based on the context in which the lambda is used.

Single Parameter Lambdas

For lambdas with a single parameter, Kotlin allows an even more concise syntax. Instead of providing an explicit name for the parameter, you can use 'it':

val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println(doubled) // Output: [2, 4, 6, 8, 10]

Here, map transforms the numbers by doubling each one, using it as an implicit name for each list element.

Returning Values from Lambdas

Lambdas can also have return values. If the last expression in your lambda is not a statement, it will be the return value:

val multiplyByTwo = { number: Int -> number * 2 }
println(multiplyByTwo(5)) // Output: 10

Notice that there's no need for a 'return' keyword. However, you can explicitly use it in labeled lambdas if needed.

Labeled Returns in Lambdas

In Kotlin, you can use labels to signify the exit point of a lambda. This is useful in nested structures:

fun foo() { 
    listOf(1, 2, 3, 4, 5).forEach lit@{ 
        if (it == 3) return@lit  
        println(it) 
    } 
    println("done with loop")
}

Here, the label @lit allows you to return only from the lambda and not the enclosing function foo().

Conclusion

Lambdas are a powerful tool in Kotlin, reducing boilerplate and making code more expressive. Understanding how to define and use them is crucial for any Kotlin programmer. Whether you're transforming collections, creating event handlers, or just passing small chunks of behavior, lambdas make your code cleaner and more versatile.

Next Article: Writing Lambdas for Anonymous Functions in Kotlin

Previous Article: Returning Functions from Functions in Kotlin

Series: Working with Functions in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin