Skip to content
DeveloperMemos

Using Lambda Expressions in Kotlin

Kotlin, Lambda Expressions2 min read

Lambda expressions are a powerful feature of functional programming that allows developers to write concise and expressive code. Kotlin, being a modern and versatile programming language, fully supports the use of lambda expressions.

In this post, we will learn what lambda expressions are, how they work, and how to use them in Kotlin.

What are Lambda Expressions?

A lambda expression is an anonymous function that can be defined without an explicit name. It is also known as a "function literal."

Lambda expressions are often used to represent a piece of code that can be passed around as an argument to other functions. They are particularly useful for implementing higher-order functions, which are functions that take one or more functions as arguments or return a function as a result.

Syntax of Lambda Expressions in Kotlin

The syntax of a lambda expression in Kotlin is as follows:

1{ argumentList -> codeBody }

Here, argumentList is a comma-separated list of parameters (if any) that the lambda expression takes, and codeBody is the actual code that the lambda expression executes.

For example, the following lambda expression defines a function that takes two integers as input and returns their sum:

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

Using Lambda Expressions in Kotlin

Lambda expressions can be used wherever a function is expected. For example, consider the following code that sorts a list of integers using a lambda expression:

1val numbers = listOf(5, 1, 3, 4, 2)
2val sortedNumbers = numbers.sortedBy { it }
3println(sortedNumbers) // prints [1, 2, 3, 4, 5]

Here, the sortedBy function takes a lambda expression that specifies the sorting criteria. The lambda expression { it } simply returns each element of the list, resulting in a sorted list.

Lambda expressions can also be assigned to variables and passed around as arguments. For example, consider the following code that defines a higher-order function that takes a lambda expression as an argument:

1fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
2 return operation(a, b)
3}
4
5val sum = { a: Int, b: Int -> a + b }
6val difference = { a: Int, b: Int -> a - b }
7
8println(calculate(5, 3, sum)) // prints 8
9println(calculate(5, 3, difference)) // prints 2

Here, the calculate function takes two integers and a lambda expression that specifies the operation to perform on them. In this case, we pass the sum and difference lambda expressions to the calculate function to add and subtract the two integers, respectively.

Advantages of Lambda Expressions

Lambda expressions have several advantages over traditional function definitions, such as:

  • Conciseness: Lambda expressions allow you to write more concise and expressive code.

  • Readability: Lambda expressions make code easier to read and understand, especially when used with higher-order functions.

  • Flexibility: Lambda expressions can be passed around as arguments and assigned to variables, making them highly flexible.

Conclusion

Lambda expressions are a powerful feature of functional programming that allows developers to write concise and expressive code. Kotlin fully supports the use of lambda expressions, which can be used wherever a function is expected. By using lambda expressions, developers can write more concise, readable, and flexible code, especially when used with higher-order functions.