[][src]Macro curry_macro::curry

macro_rules! curry {
    (|$first_arg:ident $(, $arg:ident )*| $function_body:expr) => { ... };
    (|$first_arg:ident:$first_arg_type:ty $(, $arg:ident:$arg_type:ty )*| $function_body:expr) => { ... };
    (|$first_arg:ident:$first_arg_type:ty $(, $arg:ident:$arg_type:ty )*| -> $ret_type:ty $function_body:block) => { ... };
}

Create a curried function

Example:

You can curry an add function that adds two numbers.

Simplest form, without any type annotations. You need to use the curried function so that the rust compiler can infer the input and return types for you:

let add = curry!(|a, b| a + b);
assert_eq!(add(1)(2), 3);

With input type annotations:

let add = curry!(|a: i32, b: i32| a + b);

With input and return type annotations and a block as function body

let add = curry!(|a: i32, b: i32| -> i32 { a + b });

The above three functions work the same:

// You can generate intermediate functions that are partially applied:
let add1_to = add(1);
let sum = add1_to(2);
assert_eq!(sum, 3);
// You can also can apply all arguments at once:
let sum = add(1)(2);
assert_eq!(sum, 3);