What if you could write tiny pieces of code instantly without naming or fuss?
Why Lambda syntax and declaration in Kotlin? - Purpose & Use Cases
Imagine you want to quickly create a small piece of code to use just once, like sorting a list or filtering items, but you have to write a full function with a name and all the usual parts.
Writing full functions for tiny tasks is slow and clutters your code. It's like writing a whole letter when you just want to say "Hi". This makes your code harder to read and maintain.
Lambdas let you write small, nameless blocks of code right where you need them. They are like quick notes you can pass around and use immediately, making your code cleaner and faster to write.
fun isEven(num: Int): Boolean {
return num % 2 == 0
}
val evens = list.filter(::isEven)val evens = list.filter { it % 2 == 0 }With lambda syntax, you can write concise, readable code that handles small tasks instantly without extra ceremony.
When you want to sort a list of names by length, instead of writing a full function, you can just pass a lambda that says how to compare them right inside the sort call.
Lambdas save time by avoiding full function declarations for small tasks.
They make code cleaner and easier to understand.
They help you write flexible and quick operations on collections.