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.
With Multiple Arguments
def add(a: Int, b: Int) -> Int a + b end
def multiply(a: Int, b: Int) -> Int a * b end
# 5 |> add(3) means add(5, 3)
let result = 5 |> add(3) |> multiply(2); # (5 + 3) * 2 = 16
# More complex chaining
let calc = 100
|> def(x) x - 10 end()
|> def(x) int(x / 3) end()
|> def(x) x * 4 end();
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(def(x) x % 2 == 0)
.map(def(x) x * x)
.each(def(x) print(x));
# Pipeline style
let processed = numbers
|> filter(def(x) x > 5)
|> map(def(x) x * 2)
|> reduce(def(acc, x) acc + x, 0);
print(processed); # (6+7+8+9+10) * 2 = 80
Real-World Examples
# Data processing pipeline
def process_user_data(raw_data: Hash) -> Hash
raw_data
|> def(d) d["sanitized_email"] = d["email"].lower().trim(); d end()
|> validate_user()
|> enrich_profile()
|> calculate_metrics()
end
# HTTP request pipeline
def fetch_and_process -> Hash
url
|>(url: String) http_get_json()
|> transform_response()
|> validate_data()
|> format_output()
end
# 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(def(sale) sale["quantity"] * sale["price"])
|> reduce(def(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.