Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Reactive programming and plain old closures?

Example from scala.rx:

import rx._
val a = Var(1); val b = Var(2)
val c = Rx{ a() + b() }
println(c()) // 3
a() = 4
println(c()) // 6

How is the above version better than:

var a = 1; var b = 2
def c = a + b
println(c) // 3
a = 4
println(c) // 6

The only thing I can think of is that the first example is efficient in the sense that unless a or b changes, c is not recalculated but in my version, c is recomputed every time I invoke c() but that is just a special case of memoization with size=1 e.g. I can do this to prevent re-computations using a memoization macro:

var a = 1; var b = 2
@memoize(maxSize = 1) def c(x: Int = a, y: Int = z) = x + y

Is there anything that I am missing to grok about reactive programming that provides insight into why it might be a better paradigm (than memoized closures) in certain cases?

like image 336
pathikrit Avatar asked Jun 30 '14 06:06

pathikrit


People also ask

What is the difference between reactive programming and functional programming?

Functional programming paradigm is built upon the idea that everything is a pure function. Reactive programming paradigm is built upon the idea that everything is a stream observer and observable philosophy.

What is reactive programming?

Reactive programming describes a design paradigm that relies on asynchronous programming logic to handle real-time updates to otherwise static content. It provides an efficient means -- the use of automated data streams -- to handle data updates to content whenever a user makes an inquiry.

What is reactive programming in IOS?

Reactive programming basically means relying on entities emitting elements, subscribing to its changes and reacting to those changes.

What is reactive programming in Android?

ReactiveX, also known as Reactive Extensions or RX, is a library for composing asynchronous and event-based programs by using observable sequences. This is perfect for Android, which is an event-driven and user-focused platform.


1 Answers

Problem: It's a bad example

The example on the web page doesn't illustrate the purpose of Scala.RX very well. In that sense it is a quite bad example.

What is Scala.RX for?

It's about notifications

The idea of Scala.Rs is that a piece of code can get notifications, when data changes. Usually the this notification is used to (re-)calculate a result that depends on the changed data.

Scala.RX automates the wiring

When the calculation goes over multiple stages, it becomes quite hard to track which intermediate result depends on which data and on which other intermediate results. Additionally on must recalculate the intermediate results in the correct order.

You can think of this just like a big excel sheet which must of formulas that depend of each other. When you change one of the input values, Excel has to figure out, which parts of the sheet must be recalculated in which order. When Excel has re-calculated all the changed cells, it can update the display.

Scala.RX can do a similar thing than Excel: It tracks how the formulas depend on each other on notifies the ones that need to update in the correct order.

Purpose: MVC

Scala.RX is a nice tool to implement the MVC-pattern, especially when you have business applications that you could also bring to excel.

There is also a variant that works with Scala.js, i.e. that runs in the browser as part of a HTML site. This can be quite useful if you want to dynamically update parts of a HTML page according to changes on the server or edits of the user.

Limitations

Scala.RX doe not scale when you have a huge amounts of input data, e.g. operations on huge matrices.

A better example

import rx._
import rx.ops._

val a = Var(1); val b = Var(2)
val c: Rx[Int] = Rx{ a() + b() }

val o = c.foreach{value =>
  println(s"c has a new value: ${value}")
}

a()=4
b()=12
a()=35

Gives you the following output:

c has a new value: 3 
c has a new value: 6 
c has a new value: 16 
c has a new value: 47

Now imagine instead of printing the value, you will refresh controls in a UI or parts of a HTML page.

like image 95
stefan.schwetschke Avatar answered Nov 07 '22 19:11

stefan.schwetschke