Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
Lambda expressions and anonymous functions are a significant part of Kotlin, especially due to its emphasis on functional programming. They help make the code concise, readable, and expressive.
Lambda expressions are essentially "nameless functions" that can be defined on the fly without having to formally declare them. A lambda expression in Kotlin is always surrounded by curly braces { }
.
Syntax:
{ argumentList -> codeBody }
Example:
val sum = { a: Int, b: Int -> a + b } println(sum(3, 4)) // Outputs: 7
One of the most common usages of lambdas is passing them to functions.
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) } println(operateOnNumbers(5, 3, sum)) // Outputs: 8
If the lambda is the last parameter, you can move it outside the parentheses. If it's the only parameter, you can remove the parentheses entirely.
val numbers = listOf(1, 2, 3, 4) numbers.forEach { println(it) } // "it" is the default name for a single parameter
Sometimes, you might need a function that has the behavior of a lambda but has multiple return points or behaves slightly differently. This is where anonymous functions come in.
val multiply = fun(a: Int, b: Int): Int { return a * b } println(multiply(3, 4)) // Outputs: 12
Return Behavior: In a lambda, the return
keyword will return from the nearest enclosing function, not just the lambda itself. In an anonymous function, return
will return from the anonymous function.
Syntax: Anonymous functions can have optional parameter types and an optional return type. Lambdas have a more concise syntax but don't allow explicit return types.
Kotlin provides a rich API for working with collections using lambdas. Functions like map
, filter
, reduce
, and more allow for powerful data transformations.
val doubled = numbers.map { it * 2 } val evens = numbers.filter { it % 2 == 0 }
Kotlin's type inference usually determines the type of the lambda parameters. However, you can explicitly mention them if needed.
numbers.filter { num: Int -> num % 2 == 0 }
Lambdas and anonymous functions in Kotlin simplify the syntax for declaring functions, making the code more concise. They also unlock powerful functional programming paradigms, especially when combined with Kotlin's extensive collection API. Understanding them is fundamental for effective Kotlin programming.
Using lambda expressions for concise code in Kotlin:
val sum: (Int, Int) -> Int = { a, b -> a + b } println(sum(3, 4)) // Outputs 7
Lambda parameters and type inference in Kotlin:
val square: (Int) -> Int = { it * it } println(square(5)) // Outputs 25
Single-line vs multi-line lambda expressions in Kotlin:
val singleLine: (Int) -> Int = { it * 2 } val multiLine: (Int) -> Int = { val result = it * 2 result }
Lambda expressions as function parameters in Kotlin:
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) } val result = operateOnNumbers(5, 3) { x, y -> x + y } println(result) // Outputs 8
Anonymous functions vs lambda expressions in Kotlin:
val anonymousSum = fun(x: Int, y: Int): Int { return x + y }
Defining and using anonymous functions in Kotlin:
val anonymousSum = fun(x: Int, y: Int): Int { return x + y }
Higher-order functions and lambdas in Kotlin:
fun higherOrderOperation(x: Int, y: Int, operation: (Int, Int) -> Int): Int { return operation(x, y) } val result = higherOrderOperation(4, 2) { a, b -> a * b } println(result) // Outputs 8
Capturing variables in Kotlin lambda expressions:
val factor = 3 val multiplyByFactor: (Int) -> Int = { it * factor }
Function references and method references in Kotlin:
fun multiply(a: Int, b: Int): Int = a * b val multiplyFunction: (Int, Int) -> Int = ::multiply
Lambda expressions and extension functions in Kotlin:
val numbers = listOf(1, 2, 3, 4, 5) val squaredNumbers = numbers.map { it * it }
Lambdas in Kotlin collections operations:
val numbers = listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 == 0 }