Pipe Operator
The |> operator injects the left-hand value as the first argument of the
right-hand call. This shifts reading from inside-out (f(g(h(x)))) to the
natural execution order (x |> h() |> g() |> f()), making the code self-describing.
The simplest form chains single-argument functions:
Simple and multi-line pipelines; |> with extra arguments — the left-hand value always fills the first slot.
01-pipe-basics.zolo
// Feature: Pipe operator `|>` — fundamentals
// Syntax: `value |> f()` injects `value` as the 1st argument of `f`.
// When to use: read transformations in natural order (data-first), instead
// of nesting calls. Replaces `f(g(h(x)))` with `x |> h() |> g() |> f()`.
fn double(x: int) -> int {
return x * 2
}
fn add_one(x: int) -> int {
return x + 1
}
fn square(x: int) -> int {
return x * x
}
// Simple pipeline: 3 -> 6 -> 7 -> 49.
let r = 3 |> double() |> add_one() |> square()
print(r)
// expected: 49
// Same computation without pipe — reads inside-out.
let r2 = square(add_one(double(3)))
print(r2)
// expected: 49
// Pipe with extra arguments: the LHS goes as the FIRST arg.
fn add(a: int, b: int) -> int {
return a + b
}
fn mul(a: int, b: int) -> int {
return a * b
}
// 5 |> add(10) calls add(5, 10) = 15; then mul(15, 3) = 45.
let calc = 5 |> add(10) |> mul(3)
print(calc)
// expected: 45
// Multi-line pipeline — each step stands out.
let chain = 1
|> add(2)
|> mul(10)
|> add(5)
print(chain)
// expected: 35
Challenge
Add a function negate(x: int) -> int that returns -x and insert it as the
fourth step of the chain pipeline. What is the expected result?
See also