Passing Lambdas to Functions
Pass lambdas as arguments and use trailing lambda syntax.
Functions That Accept Lambdas
A higher-order function is one that takes a function (like a lambda) as a parameter. This is how map, filter, and forEach work.
Declaring a Higher-Order Function
Use a function type as the parameter type.
fun applyTwice(n: Int, op: (Int) -> Int): Int {
return op(op(n))
}
println(applyTwice(3, { it * 2 })) // 12All lessons in this course
- What Is a Lambda? Syntax and Types
- Passing Lambdas to Functions
- it: The Implicit Parameter
- filter, map, and find on Collections