Velo by Example: Lambda Functions

Velo supports anonymous functions (lambdas) that can be stored in variables and passed around.

A lambda is declared with func without a name and assigned to a variable.

any multiply = func(int a, int b) int {
    a * b;
};

int result = multiply(4, 5);  # 20

You can also use typed function variables.

func[int] add = func(int a, int b) int {
    a + b;
};

Lambdas are commonly used with array operations like map.

array[int] numbers = new array[int]{1, 2, 3, 4, 5};

array[int] doubled = numbers.map(
    func(int index, int value) int {
        value * 2
    }
);