Higher-Order Functions
In Kotlin, you can use "higher-order functions" that accept functions as arguments or return them as values. Function types are expressed as (T) -> R, and you can obtain a function reference using ::.
Syntax
// A higher-order function that takes a function type as an argument
fun apply(x: Int, f: (Int) -> Int): Int = f(x)
// A function that returns another function
fun makeAdder(n: Int): (Int) -> Int = { x -> x + n }
// Function reference (using ::)
fun double(x: Int) = x * 2
val list = listOf(1, 2, 3)
list.map(::double) // [2, 4, 6]
// Multiple-parameter function type
fun combine(a: Int, b: Int, op: (Int, Int) -> Int): Int = op(a, b)
// Function type with no return value
fun doFor(n: Int, action: (Int) -> Unit) {
for (i in 1..n) action(i)
}
Syntax Reference
| Notation | Description |
|---|---|
| (T) -> R | A function type that takes an argument of type T and returns a value of type R. |
| (T, U) -> R | A function type with multiple parameters. |
| () -> Unit | A function type with no parameters and no return value. |
| ::functionName | Gets a reference to a top-level function. |
| ClassName::methodName | Gets a reference to a class method. |
| instance::methodName | Gets a method reference bound to a specific instance. |
Sample Code
// Takes a function type as an argument.
fun applyTwice(x: Int, f: (Int) -> Int): Int = f(f(x))
// A higher-order function that returns a function (closure)
fun makeMultiplier(factor: Int): (Int) -> Int = { x -> x * factor }
// Collection transformation (a typical use of function types)
fun transform(list: List<Int>, op: (Int) -> Int): List<Int> = list.map(op)
// Examples using function references
fun isEven(n: Int) = n % 2 == 0
fun square(n: Int) = n * n
fun main() {
// Pass a lambda to a higher-order function.
println(applyTwice(3) { it + 10 }) // 23 (3+10+10)
println(applyTwice(2) { it * 3 }) // 18 (2*3*3)
// Return a function and use it.
val triple = makeMultiplier(3)
val quintuple = makeMultiplier(5)
println(triple(7)) // 21
println(quintuple(4)) // 20
// Function references (using ::)
val numbers = listOf(1, 2, 3, 4, 5, 6)
println(numbers.filter(::isEven)) // [2, 4, 6]
println(numbers.map(::square)) // [1, 4, 9, 16, 25, 36]
// Combine functions.
val result = transform(listOf(1, 2, 3, 4), ::square)
println(result) // [1, 4, 9, 16]
// Method references
val strings = listOf("hello", "world", "kotlin")
println(strings.map(String::uppercase)) // [HELLO, WORLD, KOTLIN]
println(strings.map(String::length)) // [5, 5, 6]
println(strings.sortedBy(String::length)) // [hello, world, kotlin]
// Function type variables
val ops: Map<String, (Int, Int) -> Int> = mapOf(
"add" to { a, b -> a + b },
"mul" to { a, b -> a * b }
)
println(ops["add"]!!(3, 4)) // 7
println(ops["mul"]!!(3, 4)) // 12
}
Overview
Higher-order functions are the foundation of Kotlin's collection operations such as map(), filter(), and fold(). By accepting a function type like (T) -> R as a parameter, you can design flexible APIs where the caller decides what logic to apply.
Function references (::) are used to pass an existing function directly instead of writing a lambda. Using the ClassName::methodName form, such as String::uppercase, makes collection operation code more concise.
For lambda expression syntax, see Lambda Expressions. For extension functions, see Extension Functions.
If you find any errors or copyright issues, please contact us.