[][src]Crate fn_chain

Utilities macro/function to ease function chain to be less verbose.

This crate provide two usage style, a macro chain and a function chain.

Sample showcase

Case 1 chain by using a helper macro

use fn_chain::chain;
 
fn simple_add(a : i32, b : i32, c : i32) -> i32 {
    a + b + c
}
 
fn pass_through(v : f64) -> f64 {
    v
}
 
assert_eq!(
    6f64, 
    chain!(
        simple_add(1, 2, 3), 
        |result: i32| {(result as f64).powi(2)}, 
        |sqr: f64| {sqr.powf(0.5)},
        pass_through,
        pass_through
    )
);
// This macro will expand to:
assert_eq!(
    6f64, 
    pass_through(
        pass_through(
            (|sqr: f64| {sqr.powf(0.5)})(
                (|result: i32| {(result as f64).powi(2)})(
                    simple_add(1, 2, 3)
                )
            )
        )
    )
);

Case 2 chain by using helper function

use fn_chain::chain;
 
fn simple_add(a : i32, b : i32, c : i32) -> i32 {
    a + b + c
}
 
fn pass_through(v : f64) -> f64 {
    v
}
 
assert_eq!(6f64, *chain(simple_add(1, 2, 3))
                        .chain(|result| {(result as f64).powi(2)})
                        .chain(|sqr| sqr.powf(0.5))
                        .chain(pass_through)
                        .chain(pass_through));

Macros

chain

A macro counterpart of function chain which reduce verbosity and defer the execution until entire chain is formed.

Structs

Chainable

Chainable struct that permit user to rapidly call chain method to chain multiple function together using previous function returned value as input to the next function in chain.

Functions

chain

A function that take an expression which return a single value or object then return a Chainable then it can be chain with other function/closure with method chain of Chainable.