在Rx中,我可以创建一个主题(诸如此类):
let s = new Subject<int>()
let subscription =
s.Subscribe (fun x ->
Console.WriteLine x
)
s.OnNext 123
主题既可以观察,也可以观察。在将回调转换为流时,这很有用。
使用F#的内置Observable
模块的等效项是什么?
答案 0 :(得分:2)
在F#代码段上有一个示例实现:
Subject类型同时实现IObserver和IObservable。它在功能上等同于Reactive Extensions(Rx)库中同名类型。
答案 1 :(得分:1)
您说的是:
主题既是可观察的,又是观察者。
要实现与populatesMaze(currentX, currentY, currentCounter, mazeFinishX, mazeFinishY, mazeMatrix):
// Preventing a corner case in which our current position is outside of the maze...
if (currentX < mazeMaxtrix.lines and currentY < mazeMatrix.columns and currentX >= 0 and currentY >= 0):
// Preventing our cursor from walking over walls...
if (mazeMatrix[currentX][currentY] != 0)
mazeMatrix[currentPositionX][currentPositionY] = currentCounter
else:
return
// Until our position is not the final one, we keep going...
if (currentX != mazeFinishX and currentY != mazeFinishY ):
populatesMaze(currentpositionX + 1, currentY, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX, currentY + 1, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX - 1, currentY, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX, currentY - 1, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
实现等效的效果,您必须自己提供组成Rx.NET
接口的缺失部分。
Observable
模块中的所有值都将创建一个Subject
或最多返回两个IObserver
;它不处理IObservable
。