Skip to content

Pipe Operator ​

The |> operator passes a value into a function call. The operator is compile-time syntax and does not create a runtime pipeline object.

tyrus
const double = (n: i64): i64 => n * 2;
const increment = (n: i64): i64 => n + 1;
const clamp = (n: i64, max: i64): i64 => (n > max ? max : n);

const value: i64 = 5
    |> double
    |> clamp(%, 50)
    |> increment;

Without %, the left value is inserted as the first argument of the right call. With %, the left value is inserted exactly at the topic position.

Pipeline chains compose with closures and functional methods, making data transformations read top-to-bottom:

tyrus
const sanitize = (input: string): string => input.trim().toLowerCase();
const slugify = (text: string): string => text.replace(" ", "-");
const validate = (slug: string): string => (slug.length > 0 ? slug : "untitled");

const title: string = "  Hello World  ";
const slug: string = title |> sanitize |> slugify |> validate;

print(slug); // "hello-world"

The topic token % is useful when a function needs the piped value in a position other than the first argument:

tyrus
const log = (label: string, value: i64): void => {
    print(`${label}: ${value}`);
};

const result: void = 42 |> log("answer", %);