Pipes and partial application
Give a function fewer arguments than it takes, and you get a function waiting for the rest. |> fills parameters from the left, <| from the right; the closure is flat and costs nothing at runtime.
fn scale (factor: f64) (offset: f64) (x: f64)
-> f64:
x * factor + offset
fn main$:
// Two of three arguments given:
// a closure waiting for x.
let f = 2.0 0.5 |> scale
let readings = vec! 1.0 2.0 3.0
let scaled: Vec<f64> =
readings <- iter$
<- map (|&x| f x)
<- collect$
println! "{scaled:?}"
// All three given: a plain call.
println! "{}" (7.0 |> f)
fn scale(factor: f64, offset: f64, x: f64)
-> f64 {
x * factor + offset
}
fn main() {
// Two of three arguments given:
// a closure waiting for x.
let f = move |__hrs1| scale(2.0, 0.5, __hrs1);
let readings = vec!(1.0, 2.0, 3.0);
let scaled: Vec<f64> =
readings.iter()
.map(|&x| f(x))
.collect();
println!("{scaled:?}");
// All three given: a plain call.
println!("{}", f(7.0))
}