I would like to know how to make function composition using lambda expression. I mean, I have 2 function f(x) and g(x). How to make their composition f(g(x)) using lambda expressions? Thanks
Generic version:
static Func<T, T> Compose<T>(params Func<T, T>[] ff)
{
Func<T, T> id = x => x;
foreach (var f in ff)
{
var i = f;
var idd = id;
id = x => i(idd(x));
}
return id;
}
Due to C#'s lack of proper lexical scoping, we need a whole bunch of temporary variables with different names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With