Tzopilotl
Docs
GitHub

Tzopilotl by Example

10. Pipeline Syntax

Two Pipeline Styles

Tzopilotl supports both the |> pipeline operator and space-separated pipeline syntax for chaining function calls.

10.1 Space Pipeline

-- x f         is  f(x)
-- x f(y)      is  f(x, y)
-- x f(y, z)   is  f(x, y, z)
-- x f g       is  g(f(x))

fn square(x Int) Int = x * x;
fn collatz_len(n Int) Int {
    if (n == 1) { 0 }
    else if (n % 2 == 0) { 1 + collatz_len(n // 2) }
    else { 1 + collatz_len(3 * n + 1) }
}

-- Simple 1-arg pipeline
7 collatz_len println;        -- println(collatz_len(7))

-- With extra args
3.0 average(7.0) println;    -- println(average(3.0, 7.0))

-- Chaining with args
15 clamp(0, 10) println;     -- println(clamp(15, 0, 10))

-- Chaining multiple
6 digit_sum collatz_len println;
-- println(collatz_len(digit_sum(6)))

10.2 Pipe Operator (|>)

-- x |> f         is  f(x)
-- x |> f(y)      is  f(x, y)

3 + 4 |> collatz_len println;
-- println(collatz_len(3 + 4))

1.0 + 2.0 |> average(7.0) println;
-- println(average(1.0 + 2.0, 7.0))

Space vs Pipe

Space pipeline binds tightly (at postfix level) while |> has the lowest operator precedence. Use |> when the left side is a complex expression like a + b |> f. Use space pipeline for simple chaining: x f g.

Idiomatic Style: Prefer Left-to-Right

Write code that reads left to right, threading data through transformations, rather than nesting function calls. Rules of thumb:

-- Less idiomatic (nested):
println(toList(reverse(take(toArray((1..10)), 5))))

-- Idiomatic (left to right):
(1..10) toArray take(5) reverse toList println