Pipelines
The Pipeline Operator
Section titled “The Pipeline Operator”To prevent deeply nested function applications, Kura provides the forward pipeline operator (|>).
The pipeline operator feeds the evaluated result of its left-hand expression as the first parameter of the function invocation on its right-hand side.
fn piped(p: Position): Message => p |> Click()
// The expression above compiles exactly down to:// Click(p)Pipelines are particularly useful for sequencing long series of data modifications or layout scaling transformations without managing temporary variable states.
let processed = raw_coords |> scale_dimensions() |> center_bounds() |> Click()Pipeline Composition
Section titled “Pipeline Composition”Kura provides both forward and backward (reverse) pipeline composition.
Unlike the pipeline operator |>, the operators >> and << are purely
compositional - they construct a new function and do not trigger immediate evaluation.
Forward Pipeline Composition
Section titled “Forward Pipeline Composition”Using the forward-pipeline operator >> you can link functions together from left-to-right:
fn add(a: Int, b: Int): Int => a + bfn sub(a: Int, b: Int): Int => a - bfn mul(a: Int, b: Int): Int => a * bfn div(a: Int, b: Int): Int => a / b
let pipe = add(10) >> mul(2)let x = 10 |> pipe() // => 40
// Compiles down to:// mul(add(10, 10), 2) -> mul(20, 2) -> 40Reverse Pipeline Composition
Section titled “Reverse Pipeline Composition”Using the reverse-pipeline operator <<, you can link functions together from right-to-left:
fn add(a: Int, b: Int): Int => a + bfn sub(a: Int, b: Int): Int => a - bfn mul(a: Int, b: Int): Int => a * bfn div(a: Int, b: Int): Int => a / b
let pipe = add(10) << mul(2)let x = 10 |> pipe() // => 30
// Compiles down to:// add(10, mul(2, 10)) -> add(10, 20) -> 30Pipeline Placeholders
Section titled “Pipeline Placeholders”When a function requires the piped value at a position other than the first argument,
use the placeholder (_) to explicitly mark the target slot.
fn add(a: Int, b: Int): Int => a + bfn sub(a: Int, b: Int): Int => a - bfn mul(a: Int, b: Int): Int => a * bfn div(a: Int, b: Int): Int => a / b
let y = 10 |> mul(_, 20) // => 200// Can also be expressed as:let y = 10 |> mul(20) // => 200
let x = 10 |> div(20, _) // => 2fn add(a: Int, b: Int): Int => a + bfn sub(a: Int, b: Int): Int => a - bfn mul(a: Int, b: Int): Int => a * bfn div(a: Int, b: Int): Int => a / b
let pipe = add(1) >> div(100, _)let x = pipe(1) // => 50
// Resolves the composition down to:// div(100, add(1, 1)) -> div(100, 2) -> 50Placeholders anywhere in a stage
Section titled “Placeholders anywhere in a stage”The placeholder doesn’t have to be a whole argument. Everywhere a _ appears in the stage, the piped value goes
there, and it is computed once:
let x = 10 |> _ + 1 // 11let y = 4 |> mul(_, _) // 16Outside a pipeline, a _ is only allowed as a whole argument of a call (div(100, _)), where it leaves that
argument open.
Reverse Application
Section titled “Reverse Application”f <| x applies the function on its left to the value on its right. It is read from the right, so
f <| g <| x is f(g(x)). The two directions can’t be mixed in one chain without parentheses:
(x |> f()) |> g().