Implement function composition via operator overload


#1

Hi,

I’d like to implement the composition of lambdas (closures) so that (f >> g)(x) would means f(g(x)) and I could also use let fg = f >> g, and of course compose more than two functions, f>>g>>h>> etc.

In Haskell, it’s dead easy : fg = (f . g)

So in Rust I want to implement Shr for closures. So I started by trying to implement Shr for a struct with a closure, but it fails : https://gist.github.com/yann-ledu/81e7fc522fad4560ccbc#file-compose-rs.

  1. Can someone tell me how to implement that through struct ?
  2. Is there a way to avoid a struct and directly deal with the closure generic type ?
  3. Is there a way to allow function composition via such overloading, and thus generalize the approach to a chain of stuff with the right chaining of domain and co-domains ?

Thanks,

Yann