Developer preview. Vela, Facet, and Quire are pre-release and in active development — syntax, APIs, and availability may change, and they are not yet generally available.
SStretch Dev Docs
Vela

Functions & Lambdas

Function definitions, recursion, lambdas, closures, and higher-order workflows.

1. Named functions

fn square(x):
    x * x

fn add(a, b):
    return a + b

A function body can return implicitly from the last expression or explicitly via return.

2. Function calls

print(square(7))
print(add(3, 4))

3. Recursion

fn factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5))

4. Lambdas

Stage-0 supports compact function literals:

inc = x => x + 1
add = (a, b) => a + b

A lambda body can be any expression. Multi-arg lambdas are supported by parenthesized argument lists.

5. Captured variables (closures)

fn make_adder(n):
    return x => x + n

add10 = make_adder(10)
print(add10(5))

The returned lambda captures n from its defining scope.

6. Passing functions

fn filter(pred, xs):
    mut out = []
    for x in xs:
        if pred(x):
            out = push(out, x)
    return out

nums = [1, 2, 3, 4]
print(filter(x => x % 2 == 0, nums))

map, filter, reduce, and similar functions are in Stage-0 and used in examples.

7. Pipeline operator

x |> f turns into f(x).

result = [1, 2, 3, 4] |> filter(n => n > 2) |> map(n => n * n) |> reduce(0, (acc, x) => acc + x)
print(result)

Output:

25

8. Function-valued fields and composition

fn compose(f, g):
    x => f(g(x))

double = x => x * 2
inc = x => x + 1
print(compose(double, inc)(5))

Output:

12

9. What to remember

  • Functions are first-class values.
  • Lambdas work as arguments and return values.
  • In map, filter, and reduce, callback arity in source examples is usually one argument for map/filter and two for reducers.