Lambdas and Higher-Order Functions
Pass behavior as values.
What Is a Lambda?
A lambda is a small function you can write inline, without giving it a name.
You wrap it in curly braces: { x -> x + 1 }. Lambdas let you pass behavior around like data.
You already used them with map and filter.
Storing a Lambda in a Variable
A lambda is a value, so you can store it in a variable and call it later.
Call it like a function using parentheses.
fun main() {
val square = { x: Int -> x * x }
println(square(5))
println(square(3))
}All lessons in this course
- Lists and Mutable Lists
- Maps and Sets
- map, filter, forEach
- Lambdas and Higher-Order Functions