# Reactive Programming

For the past two days I’ve been looking into [reactive programming](http://en.wikipedia.org/wiki/Reactive_programming). Wikipedia defines it as follows:

> Reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.

But that sounds kind of abstract, to make it more clear, let’s look at a simple Javascript program:

~~~~~~~~~~
function calculateSum(l) {
var sum = 0;
for(var i = 0; i < l.length; i++) {
sum += l[i];
}
return sum;
}
var l = [2, 3, 4, 5];
var sum = calculateSum(l);
print(sum);
~~~~~~~~~~

This program will print `14`. Alright. Now let’s change the list `l` a little bit and print `sum` again:

~~~~~~~~~~
l = 6;
print(sum);
~~~~~~~~~~

We now turned the `4` in `l` into `6`. But when we print `sum` the sum hasn’t changed. This will likely not be very surprising to you. To calculate a new sum, you have to execute the `calculateSum` function again. But wouldn’t it be convenient if this happens automatically?

In spreadsheets this is exactly what happens. Open up Excel or Google Docs and fill in some numbers:

Now, in the cell below type a formula: `=SUM(A2:A5)` and push enter:

The cell now shows the sum of the numbers you entered. So far, nothing surprising. Now let’s change the `4` in that list into a `6`, just like in the Javascript program:

Hey, the cell containing the sum has changed with it! This is called reactive programming. The `SUM(A2:A5)` is not a one time calculation, but _reacts_ to changes to its parameters and recalculates if required. I feel this is a very powerful concept that has recently become more popular.

[Flapjax](http://www.flapjax-lang.org) is a language that adds this type of behavior to Javascript. Microsoft has the [reactive extensions to .NET](http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx) (Rx) that makes it easier to deal with events using LINQ. [LunaScript](http://www.asana.com/luna) is a Javascript-like language, currently under development that brings this programming model to Ajax web applications (have a look at their demo video, it’s cool). [Trellis](http://pypi.python.org/pypi/Trellis) is a Python module that adds reactive features to Python. And currently I’m also adding these features to [mobl](http://github.com/zefhemel/mobl).