Going Functional With Kotlin — Part 2: Higher-Order Functions

Raphael De Lio
3 min readAug 26, 2023

--

Twitter | LinkedIn | YouTube | Instagram

This story is part of a series.
Part 1: What is functional programming?

As we continue our journey into the world of functional programming in Kotlin, it’s time to tackle a concept that may sound imposing but is fundamentally intuitive: the higher-order function.

In this story, we’ll dive deep into the essence of higher-order functions, explore their usefulness, and understand how they offer more than just syntactic sugar. They’re tools that can drastically improve code clarity, reusability, and maintainability.

So, whether you’re a functional programming novice like me or someone looking to sharpen their Kotlin skills, come with me!

What are high-order functions?

Higher-order function may be a term you’ve never heard of before. However, it’s pretty simple to understand.

Higher-order functions are a fundamental concept in functional programming. It is a function that does at least one of the following:

  1. Takes one or more functions as arguments.
  2. Returns a function as its result.

They are useful for creating more abstract operations, manipulating functions, or creating new functions on the fly.

Let’s take a look at the example below. In this example, calculate is a higher-order function. It takes three parameters, two integers (a and b), and a function (operation).

fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int) = operation(a, b)

The parameter operation is also a function that, in turn, takes two integers and returns another integer.

Then, the calculate function applies the operation function to a and b, and returns the result.

We then use calculate with two different functions: one that adds its arguments and one that subtracts its arguments. This demonstrates how higher-order functions can be used to create more flexible and reusable code.

Try it out! Press the play button!

I encourage you to edit the code above (you can do it right here!) and implement two more functions, one for multiplying and another one for dividing the variables. Then, print out the results as well.

Another example is implementing a higher-order function for measuring how long a function takes to be processed. I particularly like this use case because it helps us encapsulate the logic for calculating the time spent in a function and reuse it whenever needed. Let’s see how we can implement it with higher-order functions.

Try it out! Press the play button!

In this example, longRunningOperation is a function that simulates a long-running operation by sleeping for 2 seconds.

We pass this function to measureTime using the :: operator. measureTime then runs the operation, measures the time before and after, and logs the elapsed time. This demonstrates how higher-order functions can be used to add behavior (in this case, logging) to other functions.

What other cool ideas could we implement with higher-order functions? Let me know in the comments!

Conclusion

In this episode of our functional programming series with Kotlin, we explored the world of higher-order functions, a powerful concept that can transform how we approach coding. These functions, which can accept other functions as arguments and/or return functions as results, provide increased code flexibility, reusability, and clarity. Our examples showcased the potential of higher-order functions to create versatile operations and even measure function execution time.

As we continue our journey into functional programming, let’s keep experimenting with higher-order functions and other functional concepts. Join me in this adventure!

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--