Look, I’ve been programming the LINQ operators in C# since they began and have been quick to adopt them, but I ALWAYS forget how to use Aggregate. I end up searching in my code for how I’ve used it in the past or going back to the same of beginner websites to remember.
Anybody have a good way to remember how to use it? Some useful mnemonic or catchphrase or mantra?
What are you forgeting?
It’s (init, (prev, next) => something)
At least that’s how I remember it. Same as fold in other languages so you pick up these similar patterns across languages.
Okay, this is nice. I can remember this. Thanks!
Aggregate
is a reducer. It reduces an Enumerable to one value by executing reducer
over each element, using the last
value and current
value, with the initial
value being used as the value for last
in the first iteration. The selector
function is optional.
or
In Javascipt, this is the reduce
method, with the arguments swapped.
You can implement Sum
using Aggregate
:
I ALWAYS forget how to use Aggregate.
I’m curious: how often do you find yourself needing it? Which aggregates are you implementing with it? I use it maybe… once every other year?
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=net-5.0