Lambdas: Dot shorthand
● Beginner3 min
In an argument, .something is short for a lambda over one value: .upper() means x => x.upper().
type Order {
amount: Int
paid: Bool
}
fn main() {
const names = ["asha", "ravi", "meenakshi"]
print(names.map(.upper()))
print(names.filter(.len() > 4))
const orders = [Order(amount: 300, paid: true), Order(amount: 120, paid: false)]
print(orders.filter(.paid).sum(.amount))
}The program ran and printed:
kavach run dot.kav
["ASHA", "RAVI", "MEENAKSHI"]["meenakshi"]300
.paid reads the paid field of each order, and .len() > 4 asks of each name whether it is longer than 4. No parameter to name, nothing to misread.