fn_chain

Crates.iofn_chain
lib.rsfn_chain
version0.1.2
sourcesrc
created_at2020-05-11 04:39:06.555922
updated_at2020-05-11 04:45:47.764386
descriptionHelper macro/function to create function chaining
homepage
repositoryhttps://github.com/NattapongSiri/fn_chain
max_upload_size
id239922
size29,701
(NattapongSiri)

documentation

README

fn_trait library

This trait provide helper function and macro called chain to ease chaining of function where output of first function piped directly as input to another function and so forth.

Use case 1 - Macro chain

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
    )
);

Use case 2 - function chain

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));
Commit count: 9

cargo fmt