#binding #argument #parameters #bind

no-std partial_application

partial function application via the partial! macro

3 unstable releases

Uses old Rust 2015

0.2.1 Jul 30, 2020
0.2.0 Sep 23, 2019
0.1.0 Feb 26, 2018

#692 in Rust patterns

Download history 83/week @ 2023-11-20 63/week @ 2023-11-27 67/week @ 2023-12-04 126/week @ 2023-12-11 95/week @ 2023-12-18 58/week @ 2023-12-25 68/week @ 2024-01-01 123/week @ 2024-01-08 140/week @ 2024-01-15 148/week @ 2024-01-22 91/week @ 2024-01-29 112/week @ 2024-02-05 271/week @ 2024-02-12 211/week @ 2024-02-19 340/week @ 2024-02-26 330/week @ 2024-03-04

1,175 downloads per month
Used in 12 crates (9 directly)

MIT/Apache

11KB
122 lines

partial_application

The partial! macro allows for partial application of a function.

partial!(some_fn => arg0, _, arg2, _) returns the closure |x1, x3| some_fn(arg0, x1, arg2, x3).
Move closures are created by adding move in front of the function: partial!(move ..)

use partial_application::partial;

// When you're using the 2015 edition of Rust, you need to import the macro like this
#[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, 42, 10, 10);
    assert_eq!(
        foo(15, 15, 10, 42, 10, 10),
        bar(15, 15)
    );
}

lib.rs:

The partial! macro allows for partial application of a function.

partial!(some_fn => arg0, _, arg2, _) returns the closure |x1, x3| some_fn(arg0, x1, arg2, x3).
Move closures are created by adding move in front of the function: partial!(move ..)

#[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)
    );
}

The expressions used to fix an argument are reevaluated on every call of the new function because of the straightforward translation behind the macro.

#
fn identity(x: u32) -> u32 { x }

let mut n = 0;
let mut f = partial!(identity => { n += 1; n});
assert_eq!(f(), 1);
assert_eq!(f(), 2);

Pre-compute arguments to be fixed in a local variable, if their creation is expensive or has unwanted side-effects.

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.

No runtime deps