如果我沒有理解這一權利:
- 當你得到比
minDuration
但比maxDuration
少多個連續真正的價值,要發出x
。
- 當您獲得的連續真值超過
maxDuration
時,您想要發出y
。
- 在任何其他情況下,不發射任何東西。
如果是這樣的話,你的大理石圖應該看起來更像是這樣的:
fffttffttfftttttttttttffftttffffttttf
1234567 123 1234 (numbers for counting guidance)
-----------------y----------x-------x (mine)
------------------y--------x-------x- (yours)
的x
只能出來的假。你不能真正發出它,因爲你不知道未來的價值會是什麼!沒有自定義操作員可以爲你解決這個問題
其餘的可以用這個來解決:
var values = new List<bool> // matches fffttffttfftttttttttttffftttffffttttf
{
false,
false,
false,
true,
true,
false,
false,
true,
true,
false,
false,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false,
true,
true,
true,
false,
false,
false,
false,
true,
true,
true,
true,
false,
};
var original = Observable.Interval(TimeSpan.FromSeconds(1))
.Where(i => i < values.Count)
.Select(i => values[(int)i]);
TimeSpan minDuration = TimeSpan.FromSeconds(3);
TimeSpan maxDuration = TimeSpan.FromSeconds(6);
var trueWindows = original
.TimeInterval()
.Scan(TimeSpan.Zero, (a, t) => t.Value
? a + t.Interval
: TimeSpan.Zero);
var coupledWindows = trueWindows.Scan(new {Previous = TimeSpan.Zero, Current = TimeSpan.Zero},
(o, t) => new {Previous = o.Current, Current = t})
.Publish()
.RefCount();
var xS = coupledWindows.Where(o => o.Previous < maxDuration && o.Previous >= minDuration && o.Current == TimeSpan.Zero).Select(t => "X");
var yS = coupledWindows.Where(o => o.Current >= maxDuration && o.Previous < maxDuration).Select(t => "Y");
至於教程,最好的資源是http://introtorx.com/。另一個比較好的是http://rxmarbles.com,雖然它使用非.NET函數名稱。
輝煌,謝謝! – ale
很好的答案,但它困擾我爲什麼「智能感知」無法處理您的代碼? :-) – supertopi
是的,現在也困擾我。我只是嘗試添加[語言標籤](http://stackoverflow.com/editing-help#syntax-highlighting),但似乎沒有任何幫助。 – Shlomo