在F#中Control.Observable和Control.Event模块之间有什么相似之处? [英] What are the similarities / differences between Control.Observable and Control.Event modules in F#?
问题描述
F#(至少在Visual Studio 2012中)同时具有 Control.Observable
和
F# (at least in Visual Studio 2012) has both Control.Observable
and Control.Event
.
- 他们是如何相关的?
- 应该使用哪一个?
- 两者之间是否存在性能差异?
- How are they related?
- Which one should be used when?
- Are there performance differences between the two?
我也想知道什么Haskell模块/包/功能.NET IEnumerable
/ IObservable
通过.NET的反向扩展实现的二元性对应于。
I would also love to know what Haskell modules / packages / features the .NET IEnumerable
/ IObservable
duality achieved with reactive extensions to .NET correspond to.
推荐答案
为了回答您问题的第一部分, IEvent
和的IObservable
。有两种类似的类型的原因是 IEvent
是为F#设计的(早期版本,大部分出于兼容性原因),而$ code> IObservable 类型稍后添加到.NET(因此,F#也添加了它的支持)。这里有一些区别:
To answer the first part of your question, there is a number of differences between IEvent
and IObservable
. The reason why there are two similar types is that IEvent
has been designed for F# (earlier and it is left there mostly for compatibility reasons) and the IObservable
type was added later on to the .NET (and so F# added support for it too). Here are some differences:
-
IEvent
不支持删除事件处理程序的,所以当您创建一个处理流程(组合map
,filter
等)然后在结果事件上调用RemoveHandler
,它会留下一些处理程序(是的,这是一个泄漏,我们写了一篇更详细的论文)
另一方面IObservable
能够删除处理程序。
IEvent
does not support removing of event handlers, so when you create a processing pipeline (combiningmap
,filter
and others) and then callRemoveHandler
on the resulting event, it leaves some handlers attached (yes, that's a leak and we wrote a more detailed paper about it) On the other handIObservable
is able to remove handlers.
作为上一点的结果,与状态组合器相比, IObservable
的行为有所不同。例如,当您使用 Event.scan
时,您可以将多个处理程序附加到生成的事件,并且它们将看到相同的状态。 IObservable
为每个附加处理程序创建一个新状态(除非您明确使用主题)。
As a result of the previous point, IObservable
behaves differently with respect to stateful combinators. For example, when you use Event.scan
, you can attach multiple handlers to the resulting event and they will see the same state. IObservable
creates a "new state" for every attached handler (unless you use subject explicitly).
在实际的F#编程中,这意味着:
In practical F# programming, this means:
-
你通常更喜欢
IObservable
如果您想要删除事件处理程序(使用RemoveHandler
或使用AwaitObservable
You should generally prefer
IObservable
if you want to be able to remove event handlers (usingRemoveHandler
or when usingAwaitObservable
in F# async workflows).
如果要声明事件(可从C#使用),则需要创建类型为 IEvent
,因此您需要使用事件
组合器。
If you want to declare events (usable from C#) then you need to create properties of type IEvent
and so you need to use Event
combinators.
如注释所述,F#模型受功能反应规划(FRP)的影响很大,这是一个Haskell首先开发的一个想法,所以你应该找到很多类似的库。 F#版本是不太纯,以便更加实用于.NET编程。
As mentioned in comments, the F# model is heavily influenced by functional reactive programming (FRP) which is an idea that was first developed in Haskell, so you should find a plenty of similar libraries. The F# version is "less pure" in order to be more practical for .NET programming.
这篇关于在F#中Control.Observable和Control.Event模块之间有什么相似之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!