Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function composition chain with a pure macro in Rust

I've read

How to compose functions in Rust?

Function composition chain in Rust

I've learned implementing a Function composition chain in Rust is rather difficult, and people use Macro with some function, however, I wonder if it's possible to use only a macro without a compose function from the first place.

I mean

compose!(f, g) can be simply reritten to |x| g(f(x)) (just another syntax)

or

compose!(f, g, h) can be similarly rewitten to |x| h(g(f(x)))

compose!(f, g, h, i) can be similarly rewitten to |x| i(h(g(f(x))))

compose!(f, g, h, i,...) can be similarly rewitten to |x| ...(i(h(g(f(x)))))

as the recursive manner.

I guess this recursive macro does not need the actual function composition function. I've just started learning Rust macro, so what would be the smart way to code this?

PS. Does the type-inference work fine with macro in Rust?

like image 563
SmoothTraderKen Avatar asked Mar 11 '26 17:03

SmoothTraderKen


1 Answers

Yes, you can do that only using macros:

macro_rules! compose {
    ($($rest:ident),+) => {
        |x| { compose!(expand x, $($rest),*) }
    };
    (expand $inner:expr, $function:ident, $($rest:ident),*) => {
        compose!(expand $function($inner), $($rest),*)
    };
    (expand $inner:expr, $function:ident) => {
        $function($inner)
    };
}

let a = compose!(f, g, h, i);
//expands into:
let a = |x| i(h(g(f(x))));
like image 61
Rubens Brandão Avatar answered Mar 14 '26 23:03

Rubens Brandão



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!