none
Using buffer with a bufferSelector - an example please?

    Question

  • Hi,

    Please could someone give a simple example of using the buffer extension using the overload where you pass a bufferSelector (Func<IObservable<TBufferClosing>>) ?

    As far as I understand, each buffer of the source will be closed and a new one started whenever the selector emits an element? However I am very new to Rx & C# so I can not seem to get it to work. Bound to be something silly.

    thanks in advance,

    Paul

     

    Wednesday, November 30, 2011 11:06 PM

Answers

  • Hi Paul. Here are some examples;

    var source = Observable.Interval(TimeSpan.FromSeconds(0.1));
    var query = from buffer in source.Buffer(() => Observable.Interval(TimeSpan.FromSeconds(1)))
    			from result in buffer.ToArray() /* buffer selector .ToArray() */
    			select result;
    

    var source = Observable.Interval(TimeSpan.FromSeconds(0.1));
    var query = from buffer in source.Buffer(() => Observable.Interval(TimeSpan.FromSeconds(1)))
    			from result in buffer.Sum() /* buffer selector .Sum() */
    			select result;
    

    This blog post goes into Buffer / Window in some detail (has diagrams etc)

    http://enumeratethis.com/2011/07/26/financial-charts-reactive-extensions/


    James Miles http://enumeratethis.com
    • Marked as answer by PaulVYoung Thursday, December 1, 2011 10:30 PM
    Thursday, December 1, 2011 4:51 AM

All replies

  • Hi Paul. Here are some examples;

    var source = Observable.Interval(TimeSpan.FromSeconds(0.1));
    var query = from buffer in source.Buffer(() => Observable.Interval(TimeSpan.FromSeconds(1)))
    			from result in buffer.ToArray() /* buffer selector .ToArray() */
    			select result;
    

    var source = Observable.Interval(TimeSpan.FromSeconds(0.1));
    var query = from buffer in source.Buffer(() => Observable.Interval(TimeSpan.FromSeconds(1)))
    			from result in buffer.Sum() /* buffer selector .Sum() */
    			select result;
    

    This blog post goes into Buffer / Window in some detail (has diagrams etc)

    http://enumeratethis.com/2011/07/26/financial-charts-reactive-extensions/


    James Miles http://enumeratethis.com
    • Marked as answer by PaulVYoung Thursday, December 1, 2011 10:30 PM
    Thursday, December 1, 2011 4:51 AM
  • Hi James,

    Thank you very much for taking the time to reply. As soon as I saw your example I had a 'ah ha!' moment. Perfect. Even better was the link to your blog post which is making very interesting reading.

    Many thanks, 

    Paul

    Thursday, December 1, 2011 10:35 PM
  • The link is broken...

    Here is a good one: https://jamesmiles.wordpress.com/2011/07/26/financial-charts-reactive-extensions/

    Thursday, January 10, 2019 11:05 AM