[−][src]Crate partial_application
The partial!
macro allows partial application of a function.
Invoking partial!(some_fn => arg0, _, arg2, _)
will return the closure
|x1, x3| some_fn(arg0, x1, arg2, x3)
.
Move closures can be created by adding move
in front of the function: partial!(move ..)
.
Due to the straightforward translation, expression arguments will be reevaluated on every call.
Pre-compute values if you don't want this.
partial!(some_fn => _, _, rand::thread_rng().gen::<u8>(), { println!("called again"); arg3 })
The closure created from the above will get a new random number and print "called again" on every call.
#[macro_use] extern crate partial_application; fn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 { (a + b*b + c.pow(3) + d.pow(4)) * mul - off } fn main() { let bar = partial!(foo => _, _, 10, _, 10, 10); assert_eq!( foo(15, 15, 10, 42, 10, 10), bar(15, 15, 42) ); }
You can also use a comma (,
) or semicolon (;
) instead of the arrow =>
.
This strange syntax choice is due to limitations imposed on us by the macro system.
No other tokens may follow the expression token for the function.
Macros
partial | The macro that creates a wrapping closure for a partially applied function |