Lambdas (Anonymous Functions)
Lambdas, or anonymous functions, are functions defined without a name. They are useful for short operations, especially when passing a function as an argument to another function. Lambdas are often used in functional programming patterns, such as with map
, filter
, or reduce
.
The syntax for a lambda is typically shorter than a named function, making them ideal for concise, inline logic.
Example: Lambda
# Lambda test: squares of a list
let nums = [1, 2, 3, 4]
let squares = nums.map(fn(x) => x * x)
print(squares)
Here, fn(x) => x * x
is a lambda that squares its input. It is passed directly to the map
function.