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.
Practice
Solution
Step 1: Understand lambda basics
Lambdas are small functions without a name that can be stored in variables.Step 2: Compare with other options
Classes, loops, and variable declarations are different concepts from lambdas.Final Answer:
A small unnamed function stored in a variable -> Option AQuick Check:
Lambda = unnamed function [OK]
- Confusing lambdas with classes
- Thinking lambdas are loops
- Mixing lambdas with variable declarations
Solution
Step 1: Recall lambda syntax
Lambdas use curly braces with parameters before an arrow, then the body: { params -> body }Step 2: Check each option
{ a, b -> a + b } matches correct syntax. (a, b) -> { a + b } wrongly uses parentheses outside braces. fun(a, b) { return a + b } is a function, not a lambda. { a, b : Int -> a + b } incorrectly places type annotation.Final Answer:
{ a, b -> a + b } -> Option DQuick Check:
Correct lambda syntax = { params -> body } [OK]
- Using parentheses instead of braces
- Trying to write full function syntax
- Incorrect type annotations inside lambda
val multiply = { x: Int, y: Int -> x * y }
println(multiply(3, 4))Solution
Step 1: Understand the lambda function
The lambda takes two Ints x and y, and returns their product x * y.Step 2: Calculate the output
Calling multiply(3, 4) returns 3 * 4 = 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Syntax errors in lambda call
val greet = { name String -> "Hello, $name" }Solution
Step 1: Check parameter syntax
Parameters must have a colon between name and type: name: StringStep 2: Verify lambda structure
The arrow -> is present after parameters, and string interpolation is allowed inside lambdas.Final Answer:
Missing colon between parameter and type -> Option CQuick Check:
Parameter syntax = name: Type [OK]
- Omitting colon in parameter declaration
- Forgetting arrow after parameters
- Thinking string interpolation is disallowed
Solution
Step 1: Understand filter usage
filter takes a lambda returning true for elements to keep. Correct condition is x % 2 == 0 for even numbers.Step 2: Check each option
val evenFilter = { list: List<Int> -> list.filter { it % 2 == 0 } } correctly uses filter with lambda and modulus check. val evenFilter = { list: List<Int> -> list.filter(it % 2 == 0) } misses braces for lambda. val evenFilter = { list: List<Int> -> list.filter { x -> x / 2 == 0 } } uses division instead of modulus. val evenFilter = { list: List<Int> -> list.filter { x -> x % 2 = 0 } } uses assignment (=) instead of comparison (==).Final Answer:
val evenFilter = { list: List<Int> -> list.filter { it % 2 == 0 } } -> Option BQuick Check:
Filter with { it % 2 == 0 } keeps evens [OK]
- Using assignment (=) instead of comparison (==)
- Using division (/) instead of modulus (%)
- Omitting braces around lambda in filter
Ready to prove it?
Pass the test and this pattern moves unknown
