Kotlin | Higher-Order Functions and Lambda Expressions

Hello, in this article, we will learn how to use Higher-Order functions and Lambda expressions in the Kotlin programming language. Thanks to these methods, we will have the ability to express simple operations, make the code understandable and customize it. In our article, we will examine concrete examples and codes related to the subject we will learn.

Kemal Adlığ
Huawei Developers
5 min readFeb 15, 2023

--

This cover photo is created by editing a Canva template.

Introduction

Don’t you want to write clean and effective code that is easy to read and understand? In this article, I’ll help you learn what Higher-Order functions and Lambda expressions are in the Kotlin programming language. Also, I’ll show you how these functions can be combined and how you can shorten your code and make it more effective. You will also learn how to use these functions efficiently.

What is a Higher-Order Function?

Higher-Order functions are functions that take a function as a parameter or return a function as an output. These functions allow us to write the code in a more understandable and powerful way. Also, these functions are used to run or customize the code that has already been written.

Higher-Order functions help to make the code readable, easy to understand, and shorter. It is also possible to run code or reuse repeated code as a single block of code. Thus, much less time is spent writing the code.

Let’s check out with an example:

In the example above, the performCalculation() function takes 3 parameters: ‘start’ and ‘end’ are the interval values of the numbers we want to calculate, and calculation is a function that takes an Int type parameter and returns an Int value. First, an array list of type Int named ‘result’ is defined. By giving the interval numbers to the calculation function as arguments, the returned values are added to the list via for loop. This list is then returned.

In order to use this Higher-Order function we defined, a list named ‘squaredNumbers’ is created and the function is called. If you noticed, we wrote the function that we need to give as an argument to our ‘performCalculation’ function as ‘{ num -> num * num }’. This is a Lambda expression. As we finish our example by printing the list, let’s take a quick look at Lambda expressions.

Lambda Expressions

Lambda expressions are simplified anonymous functions. Using these expressions you can create anonymous local functions that can pass any number of parameters and return values. These expressions are often used to express simple operations.

val variableName: (parameters) -> returnType = { arguments -> code }

variableName: The variable that will reference the lambda expression. The Kotlin language allows us to assign this expression to a variable.

paramaters -> returnType: Parameters are written in parentheses with a comma between them. Then the ‘->’ sign is added and the return type of the function is written.

{ arguments -> code }: This is the body part of the lambda expression. The arguments are separated by commas and the ‘code’ part is the part that runs when we call this function.

To illustrate with an example:

Note: If the Lambda expression you define has a single parameter, you can express it with the ‘it’ keyword.

Example of a Lambda Expression

Let’s take a look at how we can make our code more understandable and concise by using the Higher-Order function and Lambda expression with an example.

First of all, in this example, we will have a list of names, and our ultimate goal will be to convert the letters of these names to capital letters. To see the difference, let’s first define this function without using the Higher-Order function and Lambda.

Now let’s write the function we defined above with the method we learned.

As you can see, our function takes a String list and a function named ‘process’ as a parameter and returns a String type list. The function we give as a parameter, on the other hand, takes a String type variable as a parameter and returns a String variable to us. With the help of the ‘map’ function, we will be able to apply the operation we want to each list element in the Lambda expression we will define while calling the Higher-Order function.

In this way, we have written our code more effectively.

Returning A Function Using Higher-Order Function

If you remember, we said that while defining Higher-Order functions, we can give another function as a parameter, as well as that these functions can return another function. So when will this work for us? Let’s say you want to make an operation. However, this process may vary depending on the situation. The function you want to do the action must be customizable, right? It’s not practical at all to define a new function over and over for every situation. In such cases, you may need a high-order function that returns a function.

Higher-Order functions can take functions as parameters as well as return customized functions.

Let’s illustrate with a simple example:

In this example, our createGreeting high-order function takes a single parameter named ‘hour’ and returns a function that takes one String parameter and returns a String. This returned function simply greets the given time value and name. When calling the ‘createTimeGreeting’ function, a customized greet function is generated by giving the time argument. Then the returned function can be called by giving the name argument.

Conclusion

In summary, in this article, we have explained how Higher-Order functions and Lambda expressions can be used in the Kotlin programming language. Higher-Order functions are functions that take a function as a parameter or return a function. This makes the code more readable and understandable, and the code is shorter as these functions can be used in place of a lot of code. Lambda expressions are simplified anonymous functions that take any number of parameters and return a value. These expressions are often used to specify simple operations and are used in conjunction with Higher-Order functions to create more efficient and shortcode.

I hope you liked my first article. Thanks for reading and taking the time :)

References

You can also use the links below to learn more about this subject.

--

--

Kemal Adlığ
Huawei Developers

I am an ambitious software engineer intern at Huawei, working towards mastering the development of Android and Flutter applications.