如何转换 Timestamped<T>
的可观察序列到 TimeInterval<T>
的序列其中间隔是原始序列上时间戳之间的时间?
给定输入序列..
new Timestamped<int>(1, DateTime.Parse("2000-01-01 00:00:01"))
new Timestamped<int>(2, DateTime.Parse("2000-01-01 00:00:05"))
new Timestamped<int>(3, DateTime.Parse("2000-01-01 00:01:04"))
.. 输出将是:
new TimeInterval<int>(1, TimeSpan.Parse("00:00:00"))
new TimeInterval<int>(2, TimeSpan.Parse("00:00:04"))
new TimeInterval<int>(3, TimeSpan.Parse("00:00:59"))
最佳答案
我认为这行得通。
var s = source.Publish().RefCount();
var sprev = s.Take(1).Concat(s);
var scurrent = s;
var converted = Observable.Zip(sprev, scurrent, (prev, current) =>
new TimeInterval<int>(current.Value, current.Timestamp - prev.Timestamp));
我唯一不确定的是是否
Zip
当任一序列结束时结束。我认为它确实如此,但我还没有测试过。
关于.net - 将 Observable<Timestamp<T>> 转换为 Observable<Time Interval<T>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464426/