Trouble Implementing a Sliding Window in Rx

后端 未结 4 822
温柔的废话
温柔的废话 2020-12-15 09:38

I created a SlidingWindow operator for reactive extensions because I want to easily monitor things like rolling averages, etc. As a simple example, I want to s

4条回答
  •  暖寄归人
    2020-12-15 10:09

    Using your original test, with an argument of 3 for count, this gives the desired results:

    public static IObservable> SlidingWindow(
        this IObservable source, int count)
    {
        return source.Buffer(count, 1)
                     .Where(list => list.Count == count);
    }
    

    Testing like this:

    var source = Observable.Range(1, 5);
    var query = source.SlidingWindow(3);
    using (query.Subscribe(i => Console.WriteLine(string.Join(",", i))))
    {
    
    }
    

    Output:

    1,2,3
    2,3,4
    3,4,5
    

提交回复
热议问题