Introduction
A lambda expression is a small, anonymous block of code that can be treated as a value: passed to functions, stored in variables, or returned from other functions. Lambdas are central to Kotlin's functional programming style and are used extensively with collection operations like filter, map, and forEach. Unlike a named function, a lambda has no name and is typically defined inline where it's needed.
Cricket analogy: Like a franchise signing a substitute fielder for just one over who isn't on the permanent squad list, a lambda is an anonymous, disposable block of logic passed in wherever it's needed rather than a named, permanently defined function.
Syntax
val sum = { x: Int, y: Int -> x + y }
// Trailing lambda syntax when the lambda is the last parameter
val positives = list.filter { it > 0 }
// Explicit parameter type omitted, inferred from context
val doubled = list.map { n -> n * 2 }Explanation
A lambda is written inside curly braces { }. Parameters are listed before the -> arrow, and the code after the arrow is the body, whose last expression is the returned value. When a lambda is the last (or only) parameter of a function call, Kotlin allows it to be written outside the parentheses, or the parentheses to be omitted entirely — this is called trailing lambda syntax. If a lambda takes exactly one parameter, Kotlin lets you skip naming it and refer to it implicitly as it.
Cricket analogy: Like a scorer's shorthand where 'W' before the slash means wicket type and everything after is the outcome, a lambda's parameters go before the arrow and the body after it, and when it's the last argument, it's written outside the parentheses, like a trailing note on the scorecard.
Example
fun main() {
val numbers = listOf(1, -2, 3, -4, 5)
val add: (Int, Int) -> Int = { a, b -> a + b }
println(add(2, 3))
val positives = numbers.filter { it > 0 }
println(positives)
val squares = numbers.map { it * it }
println(squares)
}Output
5
[1, 3, 5]
[1, 4, 9, 16, 25]Key Takeaways
- Lambdas are anonymous blocks of code delimited by curly braces
{ }. - Parameters appear before the
->, and the body follows. - A single-parameter lambda can use the implicit name
itinstead of naming the parameter. - Trailing lambda syntax moves the lambda outside the parentheses when it's the last argument.
- Lambdas can be stored in variables typed as function types, like
(Int, Int) -> Int.
Practice what you learned
1. What is the implicit parameter name available in a single-parameter lambda?
2. In `{ x: Int, y: Int -> x + y }`, what comes after the `->`?
3. Which of these correctly uses trailing lambda syntax for `list.filter { it > 0 }`?
4. What type would correctly describe the lambda `{ a: Int, b: Int -> a + b }`?
Was this page helpful?
You May Also Like
Higher-Order Functions in Kotlin
Learn how Kotlin functions can accept or return other functions, enabling flexible, reusable, functional-style code.
Functions in Kotlin
Learn how to declare, call, and simplify functions in Kotlin, including single-expression syntax and the Unit type.
Collection Operations in Kotlin (map, filter, fold)
How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.
Extension Functions in Kotlin
Learn how Kotlin extension functions let you add new behavior to existing classes without modifying or inheriting them.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics