title: Trouble Implementing a Sliding Window in Rx
body: <p>I created a <code>SlidingWindow</code> operator for reactive extensions because I want to easily monitor things like rolling averages, etc.  As a simple example, I want to subscribe to hear mouse events, but each time there's an event I want to receive the last three (rather than waiting for every third event to receive the last three).  That's why the Window overloads I found don't seem to give me what I need out of the box.</p>
  
  <p>This is what I came up with.  I fear that it might not be the most performant solution, given its frequent List operations:</p>
  
  <pre><code>public static IObservable&lt;List&lt;T&gt;&gt; SlidingWindow&lt;T&gt;(this IObservable&lt;T&gt; seq, int length)
  {
      var seed = new List&lt;T&gt;();
  
      Func&lt;List&lt;T&gt;, T, List&lt;T&gt;&gt; accumulator = (list, arg2) =&gt;
      {
          list.Add(arg2);
  
          if (list.Count &gt; length)
              list.RemoveRange(0, (list.Count - length));
  
          return list;
      };
  
      return seq.Scan(seed, accumulator)
                  .Where(list =&gt; list.Count == length);
  }
  </code></pre>
  
  <p>It can be called this way:</p>
  
  <pre><code>var rollingSequence = Observable.Range(1, 5).SlidingWindow().ToEnumerable();
  </code></pre>
  
  <p>However, to my great surprise, instead of receiving the expected results</p>
  
  <pre><code>1,2,3
  2,3,4
  3,4,5
  </code></pre>
  
  <p>I receive the results</p>
  
  <pre><code>2,3,4
  3,4,5
  3,4,5
  </code></pre>
  
  <p>Any insights would be much appreciated!</p>