06 · Functions
Predicate short lambdas
Accesses rooted in underscore refer to the same implicit parameter. Combine fields, arithmetic and boolean tests in a callback; use an explicit lambda when the scope would be ambiguous.
On this page
Accesses rooted in underscore refer to the same implicit parameter. Combine fields, arithmetic and boolean tests in a callback; use an explicit lambda when the scope would be ambiguous.
Complete example
Predicate short lambdas.
19-short-lambda-predicates.zolo
// Short lambdas share one parameter across the argument's rooted accesses.
struct User { name: str, age: int, active: bool }
struct Line { price: int, quantity: int }
fn main() {
let users = [
User { name: "Ada", age: 36, active: true },
User { name: "Bo", age: 17, active: true },
User { name: "Cy", age: 24, active: false },
]
print(users.filter(_.active && _.age >= 18).map(_.name))
// expected: [Ada]
let lines = [Line { price: 3, quantity: 4 }, Line { price: 5, quantity: 2 }]
print(lines.map(_.price * _.quantity))
// expected: [12, 10]
let adjustment = 2
print(lines.map(_.price + adjustment))
// expected: [5, 7]
}
Rules and behavior
Function parameters and lambdas · Documentation
From a checkout of the repository:
zolo examples/features/05-functions/19-short-lambda-predicates.zolo
See also