Junior — Middle
What is the main role of lambda functions in programming?
sobes.tech AI
Answer from AI
Lambda functions in programming are anonymous functions that allow writing compact and concise code, especially when a small function is needed without the necessity of creating a full named method. They are often used to pass functions as parameters, for example, in event handlers, filtering, or sorting collections.
Example in Kotlin (Android):
val numbers = listOf(1, 2, 3, 4)
val doubled = numbers.map { it * 2 } // lambda function doubles each element
println(doubled) // [2, 4, 6, 8]