I wish we could have select that could also keep state of aggregation and continue the iteration, send the aggregation to the next select
It should be simple to add this
```C#
public static IEnumerable
{
foreach(var item in items)
{
seed = func(seed,item);
yield return seed;
}
}
////
var array = new int[]{ 1,2,3 };
var sumArray = array.SelectAggregate(0,(seed,item) => seed + item).ToArray();
// { 1,3,6 }
```
nit: Select is the equivalent to map in a number of other languages/libraries, where each transformation is supposed to be independent of each other. Perhaps a better name would be RunningAggregate
Note that, at least in database land, running aggregates are normally part of windowing functions.
Parallelization (in PLINQ) would be a pain, but likely possible.
Note that F# already has a very similar function: Seq.scan. In F#, the code above would look like this:
f#
let array = [| 1; 2; 3 |]
let sumArray = array |> Seq.scan (fun seed item -> seed + item) 0 |> Seq.toArray
Though there seems to be one difference between Seq.scan and the proposed SelectAggregate: Seq.scan returns the initial seed as the first item in the result. So, sumArray above would actually be 0, 1, 3, 6.
It could be the same. I just think the seed is something we put it directly so we know about it beforehand. It could be prepend directly so it not necessary
But it's fine to have consistency with F#. I just think it convenient
There is also System.Linq.EnumerableEx.Scan(...) of the System.Interactive package.
Nice suggestion! I want to use it!!!
But, how about rename the suggesting method to Scan?
MoreLinq that is LINQ extension library has Scan method. The method is same you suggested.
Haskell and Scala have scan methods that have same feature too.
And many Rx frameworks have Scan operator that similar to suggested method.
I think that Scan is good for Rx users and Functional language users.
In the interest of keeping the Linq API surface small, we would need to assess the popularity of such an addition before we commit to it. My impression is that fold/scan-like constructs are kind of niche in the context of an imperative language. c.f. #20330
Most helpful comment
Nice suggestion! I want to use it!!!
But, how about rename the suggesting method to
Scan?MoreLinq that is LINQ extension library has
Scanmethod. The method is same you suggested.MoreLinq Scan
Haskell and Scala have
scanmethods that have same feature too.And many Rx frameworks have
Scanoperator that similar to suggested method.I think that
Scanis good for Rx users and Functional language users.