If I’m not mistaken, Linq’s .Aggregate works like reduce() in other languages, doesn’t it?
Anyway, I’m trying this:
var tags = new[] { "a", "b", "c", ... };
tag.Rule = tags.Aggregate((x, y) => ToTerm(x) | y);
But it won’t compile because the return type of my lambda has to be a string. So how can I get this to work?
I know it looks funny, because of the types (this is going into an Irony grammar), but all you really need to know that ToTerm() returns a KeyTerm, and KeyTerms can be or’d together to make a BnfExpression, which is what tag.Rule is expecting.
You’re calling the
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)overload, which uses the first value in the sequence as the initial accumulator value.If you want the accumulator to have a different type from the elements in the sequence, you’ll need to specify the initial accumulator value yourself: