reactive programming

Uses your system voice; voice and rate in settings.

Q4354073Snapshot

≈ 5 min read

Names in other languages

  • Русскийреактивное программирование
  • ไทยการเขียนโปรแกรมแบบโต้ตอบ
  • Deutschreaktive Programmierung
  • Françaisprogrammation réactive
  • Englishreactive programming
  • fiReagoiva ohjelmointi
  • TürkçeReaktif Programlama
  • 中文响应式编程

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm, it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow. For example, in imperative programming, a := b + c would mean that a is being assigned the result of b + c at the time the expression is evaluated, and later, the values of b and c can be changed with no effect on the value of a . In contrast, in reactive programming, the value of a is automatically updated whenever the values of b or c change, without the program having to explicitly re-state the statement a := b + c to re-assign the value of a .…

In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm, it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow. For example, in imperative programming, a := b + c would mean that a is being assigned the result of b + c at the time the expression is evaluated, and later, the values of b and c can be changed with no effect on the value of a . In contrast, in reactive programming, the value of a is automatically updated whenever the values of b or c change, without the program having to explicitly re-state the statement a := b + c to re-assign the value of a . int b = 1; int c = 2; int a = b + c; b = 10; System.out.println(a); // 3 (not 12 because "=" is not a reactive assignment operator) // now imagine a special operator "$=" exists that changes the value of a variable (executes code on the right side of the operator and assigns result to left side variable) whenever explicitly initialized, and when referenced variables (on the right side of the operator) are changed int b = 1; int c = 2; int a $= b + c; b = 10; System.out.println(a); // 12 Another example is a hardware description language such as Verilog, where reactive programming enables changes to be modeled as they propagate through circuits. Reactive programming has been proposed as a way to simplify creating interactive user interfaces and near-real-time computing animation. For example, in a model–view–controller (MVC) architecture, reactive programming can facilitate changes in an underlying model being reflected automatically in an associated view. t = seconds + 1 g = (t > seconds) Because t should always be greater than seconds , this expression should always evaluate to a true value. Unfortunately, this can depend on the order of evaluation. When seconds changes, two expressions have to update: seconds + 1 and the conditional. If the first evaluates before the second, then this invariant will hold. If, however, the conditional updates first, using the old value of t and the new value of seconds , then the expression will evaluate to a false value. This is called a glitch. Some reactive languages are glitch-free and prove this property. This is usually achieved by topologically sorting expressions and updating values in topological order. This can, however, have performance implications, such as delaying the delivery of values (due to the order of propagation). In some cases, therefore, reactive languages permit glitches, and developers must be aware of the possibility that values may temporarily fail to correspond to the program source, and that some expressions may evaluate multiple times (for instance, t > seconds may evaluate twice: once when the new value of seconds arrives, and once more when t updates). t = if ((seconds mod 2) == 0): seconds + 1 else: seconds - 1 end t + 1 Every second, the value of this expression changes to a different reactive expression, which t + 1 then depends on. Therefore, the graph of dependencies updates every second. Permitting dynamic updating of dependencies provides significant expressive power (for instance, dynamic dependencies routinely occur in graphical user interface (GUI) programs). However, the reactive update engine must decide whether to reconstruct expressions each time, or to keep an expression's node constructed but inactive; in the latter case, ensure that they do not participate in the computation when they are not supposed to be active. A n →B n →A n+1 , A n →C n →A n+1 , where n=1,2... This problem could be overcome by propagating invalidation only when some data is not already invalidated, and later re-validate the data when needed using lazy evaluation. One inherent problem for reactive programming is that most computations that would be evaluated and forgotten in a normal programming language, needs to be represented in the memory as data-structures. This could potentially make reactive programming highly memory consuming. However, research on what is called lowering could potentially overcome this problem. On the other side, reactive programming is a form of what could be described as "explicit parallelism" , and could therefore be beneficial for using the power of parallel hardware.

References

Text derived from https://en.wikipedia.org/w/index.php?oldid=1374071317 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

No external citation was extracted for this revision. That is a statement about the source, not a claim that none exists.