Reactive Programming Vs Functional Programming

Bhagyashree Kulkarni
5 min readJun 15, 2021

Internet is evolving day by day and the amount of activity even in a minute on internet goes much farther than what our human brain could imagine. It can be easily stated that an app like Whatsapp daily handles the amount of traffic equivalent to entire Internet a decade ago. Picture this to get a brief idea of the activity going on as you are reading this blog

Some Facts and figures:

WhatsApp needs only 50 engineers for its 1.5 billion users over 180 countries because Erlang is used to implement its concurrency needs.

Facebook uses Haskell in its anti-spam system.

Both Erlang and Haskell are Functional programming languages.

Must have used Google Maps right. Ever wondered how efficiently the small location dot moves real time till it reaches your location?

Well that’s powered by Reactive Programming!

It is safe to say that both Functional Programming and Reactive programming paradigms are one of those few powers which make Internet what it is today.

What is Functional Programming ?

As the name suggests, functional programming revolves around building functions which are declarative and without any side effects. Elaborating more on the previous statement in simple terms, it concentrates more on the result obtained instead of focusing on ‘how to get the result’. That’s the real power of Functional Programming.

For example : Here we are taking input a list of numbers and calculating a sum of the distinct numbers in list.

Following are the core concepts of Functional Programming :

· Pure Functions

· No Side effects

· Immutable Variables

To explain these concepts, consider a simple piece of code in Java :

The above code will return different output despite of passing same arguments (10,20) because of it’s dependency on variable counter. This is an impure function having side-effect. Now consider below piece of code :

Now consider below piece of code :

This function will always print same result for same set of arguments as the result is depended only on the variables passed and no other parameter. These are called Pure functions. Functional programming is Side-effects free because of use of Pure functions. It does not depend on any State, or data change during a program’s execution. It only depends on its input arguments. Also, we are not mutating any variable just returning the sum of passed arguments at every call.

Fact : Java has always been an object oriented programming and functional paradigm to it was introduced with Lambda functions in Java8. Languages like Scala or Haskell are purely functional programming languages in nature.

What is Reactive Programming ?

Reactive programming is programming that utilizes asynchronous data streams. For example consider the Google Maps API. The real time movement of the handle is achieved by tracking your location, the acceleration of your device, and its position. Then, it combines those values and submits them to the API every few seconds. The API then returns a response which is rendered as a little moving dot on a map. And all this happens real quick in fraction of seconds.

That’s reactive programming in a nutshell.

Following are the core concepts of Reactive Programming:

· Observable

· Observer

· Subscribe

The above concepts can be easily explained by below picture:

The originator of data which can be any click stream or keystrokes is termed as ‘Observable’. We have various functions like merge, filter, map which transforms the data from these streams which is then further consumed. This consumer of data is called as ‘Observer’. In real life applications, the Observable can be any application or cloud emitting the data which can be consumed by may be a mobile application. We can also have multiple ‘Observers’. There are no specific intervals at which observer consumes data, hence the term asynchronous. An observer subscribes to the observable by calling the Subscribe() method.

Observer design pattern is the building block of Reactive Programming.

How is Reactive Programming Different from Functional Programming?

Let us consider following real life example. Suppose we have a Rest API to retrieve information from database. At a time only 100 records can be retrieved and we need to retrieve a total of 1000 records. Hence we’ll need to make 10 requests to API. Consider a following piece of code where we are simply querying the database and fetching records using just functional style of programming:

While the consumer code would look like :

The drawback here is stream will not be built until all 1000 records are fetched. This means that we need to wait until all 10 rest requests are done, and all records are in memory. So we are blocked, wasting both time and memory.

Using reactive style, we’ll modify the code as :

Here we do not accumulate the results to a stream, but pass them back to the calling code using the next method. This code will allow the calling code to start processing the data as it comes in, and you do not have to wait for all the data to load. This will save both time and memory since we do not need to have all records in memory at the same time.

If we consider above code snippets, it is seen that both Functional programming and Reactive Programming are implemented together to process data. Functional programming involves tools like filter map and reduce are used to transform the asynchronous stream while Reactive programming deals with consuming the transformed result and performing further operations. That is the main difference between two paradigms. Essentially Functional Programming is regarded as an important aspect of Reactive Programming.

Functional Programming promotes immutability making concurrent programming easier. In Reactive programming we can have multiple streams at one time or multiple observers subscribing to single data stream. Here the immutability concept of functional programming aids in making these interactions easier without the typical thread-safety and synchronization concerns.

In real life scenario both these paradigms work in tandem with each other to build powerful responsive, scalable and resilient applications!

--

--