Pipeline Operator
Chain function calls in a readable, left-to-right manner with the pipeline operator.
Basic Usage
|>
Pipeline Operator
Passes the left-hand value as the first argument to the right-hand function.
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; }
let result = 5 |> double() |> add_one() |> square();
print(result); // (5 * 2 + 1)^2 = 121
// Without pipeline (nested calls - harder to read)
let same_result = square(add_one(double(5)));
With Multiple Arguments
fn add(a: Int, b: Int) -> Int { return a + b; }
fn multiply(a: Int, b: Int) -> Int { return a * b; }
// 5 |> add(3) means add(5, 3)
let result = 5 |> add(3) |> multiply(2); // (5 + 3) * 2 = 16
// More complex chaining
let calc = 100
|> fn(x) { return x - 10; }()
|> fn(x) { return int(x / 3); }()
|> fn(x) { return x * 4; }();
print(calc); // ((100 - 10) / 3) * 4 = 120
With Collection Methods
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Method chaining with pipeline
let result = numbers
.filter(fn(x) x % 2 == 0)
.map(fn(x) x * x)
.each(fn(x) print(x));
// Pipeline style
let processed = numbers
|> filter(fn(x) x > 5)
|> map(fn(x) x * 2)
|> reduce(fn(acc, x) acc + x, 0);
print(processed); // (6+7+8+9+10) * 2 = 80
Real-World Examples
// Data processing pipeline
fn process_user_data(raw_data: Hash) -> Hash {
return raw_data
|> fn(d) { d["sanitized_email"] = d["email"].lower().trim(); return d; }()
|> validate_user()
|> enrich_profile()
|> calculate_metrics();
}
// HTTP request pipeline
fn fetch_and_process(url: String) -> Hash {
return url
|> http_get_json()
|> transform_response()
|> validate_data()
|> format_output();
}
// Complex data pipeline
let sales_data = [
{"product": "A", "quantity": 10, "price": 100},
{"product": "B", "quantity": 5, "price": 200},
{"product": "C", "quantity": 15, "price": 50},
];
let total_revenue = sales_data
|> map(fn(sale) sale["quantity"] * sale["price"])
|> reduce(fn(acc, rev) acc + rev, 0);
print("Total Revenue: $" + str(total_revenue)); // $2750
Benefits of Pipeline
Readability
Data flows left-to-right, matching natural reading order.
Debugging
Easy to add intermediate steps or breakpoints.
Composability
Build complex transformations from simple functions.
No Nesting
Avoid deeply nested function calls.