我读过维基百科上关于响应式编程的文章。我还读过一篇关于函数式响应式编程的小文章。这些描述相当抽象。
函数式响应式编程(FRP)在实践中意味着什么? 反应式编程(相对于非反应式编程?)由什么组成?
我的背景是命令式/OO语言,所以与此范例相关的解释将受到赞赏。
我读过维基百科上关于响应式编程的文章。我还读过一篇关于函数式响应式编程的小文章。这些描述相当抽象。
函数式响应式编程(FRP)在实践中意味着什么? 反应式编程(相对于非反应式编程?)由什么组成?
我的背景是命令式/OO语言,所以与此范例相关的解释将受到赞赏。
当前回答
看看Rx, net的响应式扩展。他们指出,使用IEnumerable,你基本上是从流中“拉”出来的。IQueryable/IEnumerable上的Linq查询是集合操作,从集合中“吸”出结果。但是在IObservable上使用相同的操作符,你可以编写“反应”的Linq查询。
例如,您可以编写这样的Linq查询 (from MyObservableSetOfMouseMovements中的m m.X<100 m.Y<100 选择新的点(m.X,m.Y))。
有了Rx扩展,就是这样:你有UI代码,它会对传入的鼠标移动流做出反应,并在你处于100,100框时进行绘制……
其他回答
Conal Elliott的论文《Simply efficient functional reactivity》(直接PDF, 233 KB)是一个相当好的介绍。相应的库也可以工作。
这篇论文现在被另一篇论文取代,推拉函数式反应性编程(直接PDF, 286 KB)。
关于响应式编程的简短而清晰的解释出现在Cyclejs -响应式编程中,它使用了简单和可视化的示例。
一个[模块/组件/对象]是反应性的意味着它是完全负责的 通过对外部事件的反应来管理自己的状态。 这种方法的好处是什么?这就是控制反转, 主要是因为[module/Component/object]对自己负责,使用私有方法来改进封装。
这是一个很好的起点,而不是一个完整的知识来源。从那里你可以跳到更复杂和更深入的文件。
如果你想感受一下FRP,你可以从1998年的Fran教程开始,它有动画插图。对于论文,从函数反应动画开始,然后在我的主页上的出版物链接和Haskell wiki上的FRP链接上跟踪链接。
就我个人而言,我喜欢在讨论如何实施FRP之前思考它意味着什么。 (没有规范的代码是没有问题的答案,因此“甚至没有错”。) 因此,我没有像Thomas K在另一个答案(图、节点、边、触发、执行等)中那样用表示/实现术语描述FRP。 有许多可能的实现风格,但没有一种实现说明FRP是什么。
I do resonate with Laurence G's simple description that FRP is about "datatypes that represent a value 'over time' ". Conventional imperative programming captures these dynamic values only indirectly, through state and mutations. The complete history (past, present, future) has no first class representation. Moreover, only discretely evolving values can be (indirectly) captured, since the imperative paradigm is temporally discrete. In contrast, FRP captures these evolving values directly and has no difficulty with continuously evolving values.
FRP is also unusual in that it is concurrent without running afoul of the theoretical & pragmatic rats' nest that plagues imperative concurrency. Semantically, FRP's concurrency is fine-grained, determinate, and continuous. (I'm talking about meaning, not implementation. An implementation may or may not involve concurrency or parallelism.) Semantic determinacy is very important for reasoning, both rigorous and informal. While concurrency adds enormous complexity to imperative programming (due to nondeterministic interleaving), it is effortless in FRP.
那么,什么是FRP? 你可以自己发明的。 从这些想法开始:
Dynamic/evolving values (i.e., values "over time") are first class values in themselves. You can define them and combine them, pass them into & out of functions. I called these things "behaviors". Behaviors are built up out of a few primitives, like constant (static) behaviors and time (like a clock), and then with sequential and parallel combination. n behaviors are combined by applying an n-ary function (on static values), "point-wise", i.e., continuously over time. To account for discrete phenomena, have another type (family) of "events", each of which has a stream (finite or infinite) of occurrences. Each occurrence has an associated time and value. To come up with the compositional vocabulary out of which all behaviors and events can be built, play with some examples. Keep deconstructing into pieces that are more general/simple. So that you know you're on solid ground, give the whole model a compositional foundation, using the technique of denotational semantics, which just means that (a) each type has a corresponding simple & precise mathematical type of "meanings", and (b) each primitive and operator has a simple & precise meaning as a function of the meanings of the constituents. Never, ever mix implementation considerations into your exploration process. If this description is gibberish to you, consult (a) Denotational design with type class morphisms, (b) Push-pull functional reactive programming (ignoring the implementation bits), and (c) the Denotational Semantics Haskell wikibooks page. Beware that denotational semantics has two parts, from its two founders Christopher Strachey and Dana Scott: the easier & more useful Strachey part and the harder and less useful (for software design) Scott part.
如果你坚持这些原则,我希望你能得到或多或少符合FRP精神的东西。
Where did I get these principles? In software design, I always ask the same question: "what does it mean?". Denotational semantics gave me a precise framework for this question, and one that fits my aesthetics (unlike operational or axiomatic semantics, both of which leave me unsatisfied). So I asked myself what is behavior? I soon realized that the temporally discrete nature of imperative computation is an accommodation to a particular style of machine, rather than a natural description of behavior itself. The simplest precise description of behavior I can think of is simply "function of (continuous) time", so that's my model. Delightfully, this model handles continuous, deterministic concurrency with ease and grace.
正确有效地实现这个模型是一个相当大的挑战,但那是另一个故事了。
看看Rx, net的响应式扩展。他们指出,使用IEnumerable,你基本上是从流中“拉”出来的。IQueryable/IEnumerable上的Linq查询是集合操作,从集合中“吸”出结果。但是在IObservable上使用相同的操作符,你可以编写“反应”的Linq查询。
例如,您可以编写这样的Linq查询 (from MyObservableSetOfMouseMovements中的m m.X<100 m.Y<100 选择新的点(m.X,m.Y))。
有了Rx扩展,就是这样:你有UI代码,它会对传入的鼠标移动流做出反应,并在你处于100,100框时进行绘制……
就像电子表格一样。通常基于事件驱动框架。
和所有的“范式”一样,它的新颖性是有争议的。
根据我对参与者的分布式流网络的经验,它很容易陷入节点网络状态一致性的普遍问题,即你最终会陷入很多振荡并陷入奇怪的循环中。
这是很难避免的,因为一些语义意味着引用循环或广播,并且当参与者网络收敛(或不收敛)在某些不可预知的状态时,可能会非常混乱。
类似地,尽管具有定义良好的边缘,但可能无法到达某些状态,因为全局状态偏离了解决方案。2+2可能等于4,也可能不等于4,这取决于2是什么时候变成2的,以及它们是否一直是这样。电子表格具有同步时钟和循环检测。分布式参与者通常不会。
一切都很有趣:)。