Question

I am going through the Rx framework materials through net, and I have found a plenty.

Now whenever I google around for that, I also found the "Reactive Programming" the wikipedia link.

As reactive programming is about the a := b + c, (a changes whenever either b or c change). and Rx is about subscribing to the observable sequence published by the source.

Can someone please explain what exactly the link is between reactive programming and Rx ?

Was it helpful?

Solution

What is Reactive Programming?

The difference between a "Reactive" and "Interactive" program is that, a Reactive program only executes in response to an input. Consider a very prototypical "Interactive" program, a Compiler. It takes a list of files, transforms data, spits out an output, then it's done. Fin.

Reactive programs don't really have a done - consider an Operating System. When is it "done"? It keeps running forever, responding to inputs from the outside world (network, input devices, etc). Or consider "Notepad.exe". It always has more work to do, until the user selects the "Exit" menu item. So, every GUI app you've ever written is a Reactive program - it is based on events that generally have no discrete termination.

Cool. Where does "Functional" come in?

One of the most important aspects of Functional Reactive programming, is that events are first-class. This means, that you can create an Event and just assign it to a variable. In Rx, the Type of this variable is IObservable<T>. Because events are Just Variables, we can combine them together to build more interesting events.

We can take those events and combine them even further too, and use them to build even more interesting events, or we can build operators that encapsulate behaviors like "Timeout" or "Retry", without tying ourselves to what kind of event the source is.

OTHER TIPS

Reactive Extensions is one way of achieving Functional Reactive Programming using a classical non-purely-functional language, such as a C#. You can, however, program in any style when using Rx, but it takes discipline (and sometimes ignorance) to achieve a purely-functional-reactive programming style using Rx.

To achieve FRP using Rx, we have to take advantage of the provided types and high level functions...

var a = new BehaviorSubject<int>(10);
var b = new BehaviorSubject<int>(5);

var c = Rx.Observable.CombineLatest(a, b, (a, c) => a + b);

It would, however, by more intuitive (but not possible with Rx) to write it like so...

behavior a = 5;
behavior b = 10;
behavior c = a + b;

Rx is not a first-order language. It lives inside of languages. Therefore, it cannot impose restrictions on the way you code. It is only a tool to help you achieve FRP, and therefore you must know FRP before you can achieve it using Rx.

That being said, you could use the composability that Rx provides without programming in a purely-function-reactive style, which is what a lot people end up doing, usually for convenience.

Reactive programming is an old term that is simply another way to say "dataflow programming". It is a style of programming where the data controls execution as opposed to flow-control (used in all common programming languages like C#) where the developer explicitly defines execution flow using "if" or "for" constructs.

Reactive programming does not always mean Functional Reactive Programming (FRP). FRP is a term coined by Conal Elliott from everything I have found.

Reactive Extensions is one implementation of dataflow in that it allows programs to react to a stream of events using LINQ operators instead of using callbacks.

Consider your example of a = b + c. That is an instantaneous calculation whereby a is assigned a value based only on the current value of each of b and c.

Now, to be reactive we wish to change the value of a whenever the values of b and c change. So we're no longer considering an instantaneous calculation, instead we're watching a series of values for b and c, that vary in time, and producing a series of values for a that also vary in time.

A series that varies in time in Rx is an IObservable<T>.

So if we start with the sequences of bs and cs being each of type IObervable<int> we can generate a series of @as like so:

var @as = bs.CombineLatest(cs, (b, c) => b + c);

Now I can obtain the latest value from @as like so:

@as.Subscribe(a =>
{
    /* do something with a */
});

That's (an example of) reactive programming using Rx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top