Trouble Implementing a Sliding Window in Rx
Just source.Window(count, 1)
- or source.Buffer(count, 1)
It be a window/buffer of "count" items, sliding by one.
Using your original test, with an argument of 3 for count, this gives the desired results:
public static IObservable<IList<T>> SlidingWindow<T>(
this IObservable<T> 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