Skip to content
Table of contents
Higher-Order Function

Higher-Order Function

1 min read #kotlin / #functional programming

What is it? If you are coming from JavaScript, this concept may sound familiar to you. Otherwise, here is what it means.

If a function can accept another function as arguments and produces functions as return values, then that programming language supports higher-order function.

Using Lambda

In Kotlin, you can store a lambda in a reference.

val isPositive: (Int) -> Boolean = { it > 0 }

listOf(1, 0, -1).any(isPositive)
// true
kotlin

Using References

We talked about property references in replacement of lambda.

In the example below, we will also look at how you can make use of generic function as argument in higher-order function.

Summary

Higher-order functions are an essential part of functional programming languages. In Kotlin, we have used it in many places using the built-in higher-order functions such as filter(), map() and any().