I am trying to come up with a linq query to convert an IEnumerable<int> to another IEnumerable<int>, where each int in the result is the sum of all the ints up to that position from the initial list:
Given int[] a
I need int[] b
Where b[0] = a[0], b[1] = a[0] + a[1], b[2] = a[0] + a[1] + a[2] and so on
Alternatively, the sums above can be written as b[1] = b[0] + a[1], b[2] = b[1] + a[2] and so on, but I don’t see how that would help.
I can, of course, do this with a for loop, but I obtain the a[] sequence from a query and I thought it would look nicer if I continue that query instead of suddenly adding a for there 🙂
Well, you can do it with side effects easily enough, although it’s pretty icky…
It would be nice if the framework provided a sort of “running aggregate” to encapsulate this, but it doesn’t as far as I’m aware.