4 ms·
C# Aggregate(..) extension method (linq)?
by ed_blackburn 5y ago
C# Aggregate(..) extension method (linq)?
- kaba0 5y agoThat’s just reduce. Teeing is basically creating multiple streams from a single one and after doing something with those “branches”, aggregating them.
- jayd16 5y agoAggregate does the job just fine in this case. Multiple streams are not needed. .Aggregate(new PriceAndRows(), //here result is the PriceAndRows instance and next is a cart row. (result,next) => { result.Price+=next.Price; result.Rows.Add(next); return result; }) I'm trying to think of a better use case where multiple collectors would be cleaner than LINQ but LINQ has a lot of tools in the toolbox, SelectMany, Aggregate, temporary anonymous types, etc. However, even in the Java side, the example could be done with reduce alone, I think.
- kaba0 5y ago> However, even in the Java side, the example could be done with reduce alone, I think. That’s my point: teeing itself is not aggregate/reduce. If after the branching the streams differ in size, reduce no longer applies, for example.
- jayd16 5y agoBut my point is that Aggregate and reduce can handle that in this case because they both can simply add to the PriceAndRows instance incrementally. You can sum the total and build the list in the body of the Aggregate/reduce method. Teeing is pointless here. There's no need to use another collector.