How to aggregate delegates into a new delegate with linq

aggregate, c#, functional-programming, linq

Solution

Do it like this:

Func<object, bool> aggregate = o => functions.All(f => f(o));

Of course this is cheating a little because the functions happen to return `bool` so we can use `Enumerable.All` directly to produce an aggregate result. Tthis also has the side effect that not all functions in the list will be called -- as soon as one returns `false` we pack up and leave.

In the general case this kind of processing is done with `Enumerable.Aggregate`, which could go like this:

Func<object, bool> aggregate = o => 
    functions.Select(f => f(o))
             .Aggregate(true, (result, @partial) => result && @partial);

Problem

Let's say I have a `IEnumerable<Func<object, bool>>`. I want to make a new `Func<object, bool>` which should return true if every function of that list returns true when called on some object. In other words I want to aggregate (reduce\foldl) a list of functions.

Original source