How to get Sum of “last” items of N hot Observable instances?

后端 未结 2 446
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 04:56

EDIT: On 09/15/2013 - I am describing my scenario further broken into steps to help everybody understand my situation better. Added the source for whole application for down

相关标签:
2条回答
  • 2020-12-11 05:31

    The Rxx library has an overload of CombineLatest() which takes an IObservable<IObservable<T>>. If you use this overload, then the solution is easy:

    var runningSum = allCqis
        .Select(cqi => cqi.StartWith(0)) // start each inner sequence off with 0
        .CombineLatest() // produces an IObservable<IList<decimal>>
        .Select(cqis => cqis.Sum()); // LINQ operator Sum(IEnumerable<decimal>)
    

    Looking at the source code for Rxx.CombineLatest might be useful to see how the problem is solved "under the hood"

    0 讨论(0)
  • 2020-12-11 05:37

    Lots of questions! Maybe time to brush up on your Rx skillz? Pretty much all of your recent questions are covered in my web site IntroToRx.com. Having a deep understanding of Rx, will allow you to answer these fairly simple questions much more quickly than asking on a forum. You should be able to read the book in less than 3 days.

    Anyway....

    1 Do you want a running sum or just a single sum at the end?

    2 Then, do you want a sum for all values for all streams, or the sums of each stream?

    To get a single sum value for a sequence you use the .Sum() operator. http://introtorx.com/Content/v1.0.10621.0/07_Aggregation.html#MaxAndMin

    To get a running total, use the Scan operator. http://introtorx.com/Content/v1.0.10621.0/07_Aggregation.html#Scan

    So the answer is probably something like this (untested):

    sources.Select(source=>source.Scan(0m, (acc, value)=>acc+=value)).Merge();
    
    0 讨论(0)
提交回复
热议问题