A lambda is a function without a name that you can pass around as a value:
val square: (Int) -> Int = { x -> x * x }
println(square(5)) // 25
A higher-order function is one that takes a function as a parameter or returns one. You’ve already used these constantly — setOnClickListener { ... } is a higher-order function call.
fun List<Int>.sumIf(predicate: (Int) -> Boolean): Int {
var total = 0
for (item in this) {
if (predicate(item)) total += item
}
return total
}
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evenSum = numbers.sumIf { it % 2 == 0 } // 12
That’s also an extension function — sumIf is added onto List<Int> without modifying the original class. Extension functions are how Kotlin adds conveniences to existing Java/Android classes (like String or View) without inheritance.
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information