The Breakdown of Functional Reactive Programming

OK, Now that I have you on my side of the table, we are going to break down Functional Reactive Programming to understand clearly all it’s aspects; what it is and how will we implement this in our project using RxJava. It is made of two parts: Functional Programming and Reactive programming.

What is Functional Programming?

Typically, most of us have an imperative mindset. We see things as objects which bring together two things: data and functions that act on that data. In OOPS paradigm data can be changed or mutated. But in Functional Programming we code the program as such that we try to avoid data mutation. Instead we create a new set of data every time we need to mutate the data. This helps in the way that a function that transforms the data set will always return the same value as the original data set is never mutated. Thus this function is easily unit-testable.

Difference Between Imperative and Functional Approach

OK, I know, I know, you want an example. Suppose you have a generic thing called Figure. Square, circle, triangle are all Figures.Now you have a box containing 3 squares.You need to convert them all to circles applying some operations. The imperative approach to solve this is iterate through the list and convert each item from a square to a triangle and then to a circle one by one.

Imperative approach to problem: Given a data set( list) of 3 squares convert it to data set of circles using operators.

However the functional approach differs as such that we put our 3 items in a stream. Then we convert this stream of squares into a stream of triangles and then this stream into a stream of circles.

Functional approach to problem: Given a data set( list) of 3 squares convert it to data set of circles using operators.

Both of the paradigm are achieving the same result, it is just a matter of how it is done. For Asynchronous and some other operations an imperative approach has many problems and that is why we are switching to a functional approach.

Pure functions

Functional Programming introduces a very cool concept of Pure functions. This blog post explains this beautifully. This is the implementation of FRP without using RxJava and is very powerful. The summary of the post is this;

Consider the function:

function a()

{

item = queue.getItem();

process(item);

print(item.name);

}

The function has a hidden input: ‘queue.getItem( )’. It is called hidden because looking at the function signature one cannot determine that it is required by the function. This is known as a Side cause. Similarly it is printing the item name. This also cannot be predicted by the signature and hence is a hidden output called as Side effect.

As you can imagine Side effects and Side causes lead to numerous problems and bugs that can’t be easily tracked as they are hidden. They also lead to testing problems.

Any function which doesn’t have a hidden input or output is known as a Pure function. For pure functions, looking at the function signature, we can determine what it’s inputs and outputs are. Example:

function int add ( Integer a, Integer b)

{

c = a+b;

return c;

}

Functional Programming is programming using pure functions and removing side effects are much as possible. Functional languages are languages which encourages and provides easy way to do functional programming.

When to use Functional Approach?

My view is that Functional programming is better to deal with the data of people while OOPS is better when we deal with people. In a MVP pattern the model classes should use the OOP paradigm while the funtions inside these classes should use Functional approach. Transformation or manager classes (Like presenter or views in MVP) should also use pure functions to operate on model objects having pure functions. The benefits of both world! Using functional approach we can gain the advantages listed above.

What is Reactive Programming?

Reactive Programming is the paradigm that says that our code should be as such that we react to changes. A very good explanation of reactive programming is given in this medium post. A good example from this post is: Suppose you order a coffee and while waiting you focus shifts and you start reading a medium article. Now as soon as your order comes up and your name is called you react to this change and your focus changes from the article to coffee.This is reactive programming.

What is Functional Reactive Programming?

A mixture of Functional and Reactive Programming is called Functional Reactive Programming. In this case we code our project as such that we react to changes using the functional paradigm. A very good resource for understanding FRP is this gist by Andre Stalz. To understand FRP lets see the mixing of Functional Programming and Reactive Programming using an example:

Button click stream example for FRP

Suppose we have to listen for double or more taps on a button. We can see in the marble diagram that we first have the event stream of user clicking the button. This event stream is a functional programming data set. First we use RxJava’s operator called buffer and throttle the click stream.This operator will merge/accumulate all the events for the given parameter time (250ms) and adds them as one. To make it very clear in terms of FRP, we used pure functions from RxJava to have a new data set which has mutated from the original one. Next we map these click events as number of clicks to the data set. Again we have a new mutated data set which we have created using RxJava’s functional approach . Then we filter our data set for items greater than or equal to 2. Now we react to these clicks data set using the reactive paradigm.

To summarize this example, we used pure function operators of RxJava to mutate our button click stream (from click stream to accumulated stream to number of click stream and then filtered it) and then we reacted to the mutated button click stream. This paradigm is FRP as we used functional approach to do reactive programming.