Pipeline Operator¶
The pipeline operator (|>) allows you to chain function and method calls in a readable, left-to-right flow. It passes the result of the expression on the left as the first argument to the function or method on the right.
Syntax¶
Is equivalent to:
Examples¶
With Functions¶
def add(a: int, b: int) -> int:
return a + b
def square(x: int) -> int:
return x * x
let result = 5 |> add(3) |> square()
# Equivalent to: square(add(5, 3))
# (5 + 3)^2 = 64
With Methods¶
The pipeline operator also works with class methods. The pipe value is injected as the first explicit argument.
class Calculator:
pub def sub(self, a: int, b: int) -> int:
return a - b
let calc = Calculator()
# 10 is passed as 'a', 5 is passed as 'b'
let res = 10 |> calc.sub(5)
# Equivalent to: calc.sub(10, 5)
# Result: 5
Chaining¶
Pipelines are excellent for deep chaining of operations, improving readability by avoiding deeply nested parentheses.