Pipe Operator (|>)
The pipe operator (|>
) in OSL allows you to chain function calls in a more readable way by passing the result of one expression as the input to another function. This creates a data pipeline that flows from left to right.
Syntax
This is equivalent to:
Description
The pipe operator takes the value on its left side and passes it as the first argument to the function on its right side. This allows you to create chains of operations that are more readable than nested function calls.
Key benefits of using the pipe operator:
Improves code readability by showing data flow from left to right
Reduces nesting of function calls
Makes complex transformations easier to understand
Important Constraints
In OSL, the pipe operator must be passed a function object directly, not a function call with parameters. This is because:
The pipe operator expects a function object on its right side
If you include parameters, the function would be executed first, and its return value (not the function itself) would be passed to the pipe
For example:
Examples
Basic Usage
Chaining Multiple Operations
The pipe operator really shines when you need to apply multiple transformations in sequence:
Working with Functions That Need Additional Parameters
If you need to use a function that requires additional parameters with the pipe operator, you need to create a wrapper function:
Working with Arrays
The pipe operator is particularly useful when working with array transformations:
Notes
The pipe operator passes the left value as the first argument to the function on the right
The function on the right must be a function object, not a function call with parameters
To use functions with additional parameters, create wrapper functions or use anonymous functions
The pipe operator has lower precedence than most other operators
You can chain multiple pipe operations:
value |> func1 |> func2 |> func3
The pipe operator works with both named functions and anonymous functions
Last updated
Was this helpful?