Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

functional-programming - 什么是(功能)反应式编程?(What is (functional) reactive programming?)

I've read the Wikipedia article on reactive programming .

(我已经读过关于反应式编程的维基百科文章。)

I've also read the small article on functional reactive programming .

(我还阅读了关于功能反应式编程的小文章。)

The descriptions are quite abstract.

(描述非常抽象。)

  1. What does functional reactive programming (FRP) mean in practice?

    (功能反应式编程(FRP)在实践中意味着什么?)

  2. What does reactive programming (as opposed to non-reactive programming?) consist of?

    (反应式编程(与非反应式编程相反?)由什么组成?)

My background is in imperative/OO languages, so an explanation that relates to this paradigm would be appreciated.

(我的背景是命令式/ OO语言,因此可以理解与此范例相关的解释。)

  ask by JtR translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to get a feel for FRP, you could start with the old Fran tutorial from 1998, which has animated illustrations.

(如果你想要了解FRP,你可以从1998年的旧Fran教程开始,该教程有动画插图。)

For papers, start with Functional Reactive Animation and then follow up on links on the publications link on my home page and the FRP link on the Haskell wiki .

(对于论文,从功能反应动画开始,然后跟进我的主页上的出版物链接和Haskell维基上的FRP链接上的链接。)

Personally, I like to think about what FRP means before addressing how it might be implemented.

(就个人而言,我想在解决FRP如何实施之前考虑一下FRP的含义 。)

(Code without a specification is an answer without a question and thus "not even wrong".) So I don't describe FRP in representation/implementation terms as Thomas K does in another answer (graphs, nodes, edges, firing, execution, etc).

((没有规范的代码是一个没有问题的答案,因此“甚至没有错”。)因此,我没有像Thomas K在另一个答案中所做的那样用表达/实现术语描述FRP(图形,节点,边缘,触发,执行,等等)。)

There are many possible implementation styles, but no implementation says what FRP is .

(有许多可能的实现方式,但没有实现FRP说什么 。)

I do resonate with Laurence G's simple description that FRP is about "datatypes that represent a value 'over time' ".

(我确实与劳伦斯G的简单描述产生共鸣,即FRP是关于“随时间变化代表价值的数据类型”。)

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 直接捕获这些不断发展的价值并且对于不断发展的价值没有任何困难。)

FRP is also unusual in that it is concurrent without running afoul of the theoretical & pragmatic rats' nest that plagues imperative concurrency.

(FRP也是不同寻常的,因为它并没有与理论上和实用的老鼠的巢穴发生冲突,这种老鼠的巢穴困扰着势在必行的并发。)

Semantically, FRP's concurrency is fine-grained , determinate , and continuous .

(从语义上讲,FRP的并发性是细粒度的确定的连续的 。)

(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中毫不费力。)

So, what is FRP?

(那么,什么是FRP?)

You could have invented it yourself.

(你可以自己发明它。)

Start with these ideas:

(从这些想法开始:)

  • Dynamic/evolving values (ie, 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", ie, continuously over time.

    (通过应用n-ary函数(在静态值上),“逐点”,即连续地随时间推移来组合n个行为。)

  • 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.

    (因此,你知道自己处于坚实的基础,使用指称语义技术给整个模型一个组合基础,这意味着(a)每种类型都有相应的简单和精确的“意义”数学类型,并且( b)每个原语和运算符具有作为组成部分含义的函数的简单和精确的含义。)

    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 .

    (如果这个描述对你来说是胡言乱语,请参考(a) 具有类型态射的指称设计 ,(b) 推挽功能反应式编程 (忽略实现位),以及(c) Denotational Semantics Haskell wikibooks页面 。)

    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.

    (请注意,指称语义有两个部分,来自它的两位创始人Christopher Strachey和Dana Scott:更容易和更有用的Strachey部分以及更难和更少用途(用于软件设计)Scott部分。)

If you stick with these principles, I expect you'll get something more-or-less in the spirit of FRP.

(如果你坚持这些原则,我希望你会在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.

(令人欣喜的是,这个模型轻松而优雅地处理连续,确定的并发。)

It's been quite a challenge to implement this model correctly and efficiently, but that's another story.

(正确有效地实施这个模型是一个相当大的挑战,但这是另一个故事。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...