Is there a practical difference when using the LINQ Aggregate
method by passing a resultSelector function or by directly passing the Aggregate
result value to the function?
Code example (there's better way to do that, but this illustrates the problem pretty well):
var list = new string[] { "a", "b", "c" };
list.Aggregate(new StringBuilder(), (sb, s) => sb.AppendLine(s), sb => sb.ToString());
list.Aggregate(new StringBuilder(), (sb, s) => sb.AppendLine(s)).ToString();
In the end, both statements return the same string. Is there code that can be written one way, but not the other?
As far as I'm aware, there is no difference. Looking at my Edulinq implementation, I've implemented the overload without the selector just by calling the overload with a selector, and then with an identity transformation:
public static TAccumulate Aggregate<TSource, TAccumulate>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
{
return source.Aggregate(seed, func, x => x);
}
It would have been perfectly reasonable to do it the other way round, e.g.
public static TResult Aggregate<TSource, TAccumulate, TResult>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func,
Func<TAccumulate, TResult> resultSelector)
{
var intermediate = source.Aggregate(seed, func);
return resultSelector(intermediate);
}
Now just because they're equivalent doesn't mean the overload is useless. For example, sometimes it could be easier to express the result selector using a lambda expression:
var result = list.Aggregate(..., ..., total => total < 0 ? total + 1
: total - 1);
You could create a separate method (or just a Func<TArg, TResult>
) to do that, but in some cases you want to do the whole thing in a single method call.
In short - use whichever approach is most convenient for you in each individual case; they're equivalent as far as I'm aware.
See more on this question at Stackoverflow