2016-01-19 60 views
1

元素的序列,以及兩個TimeSpan閾值,minDurationmaxDuration,我需要改造,使得序列:識別具有最小和給定一個<code>IObservable<bool></code>最大持續時間

  • 真值的序列跨越一個時間在minDurationmaxDuration之間變換爲「x」
  • 將超過maxDuration轉換爲「y」的真值序列; 「Y」 maxDuration時間後,因爲第一個真正的價值是發出

爲了讓事情更清晰傳遞拉高,假設minDuration = 3maxDuration = 6,並假設項目以每秒一個的速度發出:

fffttffttfftttttttttttffftttffffttttf

------------------y--------x-------x-

我的猜測是,我需要實現一個自定義操作,但作爲一個新手,RX,我不知道怎麼了,我有一個很難找到EXA除了使用擴展方法組合現有的運算符之外,

歡迎鏈接到教程和實現自定義操作符的示例。

回答

2

如果我沒有理解這一權利:

  1. 當你得到比minDuration但比maxDuration少多個連續真正的價值,要發出x
  2. 當您獲得的連續真值超過maxDuration時,您想要發出y
  3. 在任何其他情況下,不發射任何東西。

如果是這樣的話,你的大理石圖應該看起來更像是這樣的:

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函數名稱。

+1

輝煌,謝謝! – ale

+0

很好的答案,但它困擾我爲什麼「智能感知」無法處理您的代碼? :-) – supertopi

+0

是的,現在也困擾我。我只是嘗試添加[語言標籤](http://stackoverflow.com/editing-help#syntax-highlighting),但似乎沒有任何幫助。 – Shlomo

相關問題