Please help me understand a phenomenon :
Why is X NOT equal to index in the Observable's items ?
Building blocks for example :
public class EcgSample
{
public EcgSample(int y)
{
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}
private void Print(Tuple<EcgSample, int> s)
{
Debug.WriteLine("X : {0} , Y : {1} , Index : {2}", s.Item1.X, s.Item1.Y, s.Item2);
}
private List<EcgSample> CreateSamples()
{
var testSamples = new List<EcgSample>();
for (short i = 0; i < 1400; i++)
{
testSamples.Add(new EcgSample(i));
}
return testSamples;
}
Example observable : (Which outputs an expected result)
// (1) Create From Collection .
IObservable<EcgSample> sampleObservable = CreateSamples().ToObservable(new EventLoopScheduler());
// (2) Repeat
IObservable<EcgSample> repeated = sampleObservable.Repeat();
// (3) Indexed
IObservable<Tuple<EcgSample,int>> indexed = repeated.Select((item, index) =>
{
item.X = index;
return new Tuple<EcgSample, int>(item, index);
});
// (4) Buffered
IObservable<IList<Tuple<EcgSample, int>>> buffered = indexed.Buffer(250);
// (5) SelectMany and Print .
_disposable = buffered.SelectMany(buf => buf).Subscribe(Print);
OUTPUT : This is the expected output of the Observable sequence .
[8384] X : 0 , Y : 0 , Index : 0
[8384] X : 1 , Y : 1 , Index : 1
[8384] X : 2 , Y : 2 , Index : 2
[8384] X : 3 , Y : 3 , Index : 3
[8384] X : 4 , Y : 4 , Index : 4
Modification : (Which DOES NOT outputs an UN-Expected result)
Now i wan't each buffer to be taken every interval :
// (5) Create an Observable from a Timer.
IObservable<ElapsedEventArgs> timerObservable = Observable.Create<ElapsedEventArgs>(
observer =>
{
var timer = new Timer();
timer.Interval = 250;
timer.Elapsed += (s, e) => observer.OnNext(e);
timer.Start();
return Disposable.Create(() =>
{
timer.Stop();
});
});
// (6) Zip with the buffer observable
IObservable<IList<Tuple<EcgSample, int>>> zipped = timerObservable.Zip(buffered, (t, b) => b);
// (7) SelectMany and Print .
_disposable = zipped.SelectMany(buf => buf).Subscribe(Print);
OUTPUT : This outputs an unexpected result : notice that X is not equal to index.
[9708] X : 187600 , Y : 0 , Index : 0
[9708] X : 187601 , Y : 1 , Index : 1
[9708] X : 187602 , Y : 2 , Index : 2
[9708] X : 187603 , Y : 3 , Index : 3
Any ideas why X starts at 187600 ( Needless to say this value is different every time i run my program) ..?
EDIT :
I Solved the issue by simply projecting at the end , but i would still like to know why the first issue occurs .
List<EcgSample> list = CreateSamples();
var loop = new EventLoopScheduler();
var sampleObservable = list.ToObservable(loop);
IObservable<EcgSample> reapeted = sampleObservable.Repeat();
IObservable<IList<EcgSample>> buffered = reapeted.Buffer(250);
IObservable<ElapsedEventArgs> timerObservable = Observable.Create<ElapsedEventArgs>(
observer =>
{
var timer = new Timer();
timer.Interval = 250;
timer.Elapsed += (s, e) => observer.OnNext(e);
timer.Start();
return Disposable.Create(() =>
{
timer.Stop();
});
});
IObservable<IList<EcgSample>> zipped = timerObservable.Zip(buffered, (t, b) => b);
_disposable = zipped.SelectMany(buf => buf).Select((item, index) =>
{
item.X = index;
return new Tuple<EcgSample, int>(item, index);
}).Subscribe(Print);