在F#中Control.Observable和Control.Event模块之间有什么相似之处? [英] What are the similarities / differences between Control.Observable and Control.Event modules in F#?

查看:111
本文介绍了在F#中Control.Observable和Control.Event模块之间有什么相似之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 does not support removing of event handlers, so when you create a processing pipeline (combining map, filter and others) and then call RemoveHandler 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 hand IObservable 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 (using RemoveHandler or when using AwaitObservable 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆