Why ReactiveX is the only Functional Programming library you must Learn

Image for post
Image for post

An overview of Functional Programming

Ask any programmer who has heard about Functional Programming (FP) and you’re likely to get one of two reactions: either they have tried it and love it, or they have heard of these mythical creatures called functional programmers that eschew the use of classes and objects in favor of an approach full of functions. Functions everywhere.

It’s not nearly as bad as it sounds if you’re staunchly in the Object Oriented Programming (OOP) camp. Functional programming made me a much better programmer. In case you can’t see yourself leaving the OOP world even a little, this great article is required reading before moving on to understand the common sticking points of a purely Object Oriented approach. After reading that article, I went from being an OOP-enthusiast to becoming genuinely curious about functional programming. Having a JavaScript background made me appreciate the power of functions that I was already using as promises and callbacks from back in the ES5 days.

There are plenty of articles talking about the benefits of functional programming, but most of them focus on purely functional languages like Haskell, Standard ML, Scheme, or Erlang. While many of these articles do a great job of providing an introduction to functional programming, their approach was too theoretical. During my learning journey for functional programming, I wanted to stay within the JavaScript ecosystem because that’s what I’m familiar with and I knew that JavaScript would have a node module for any flavor of functional programming. I found something even better! A Mostly Adequate Guide to Functional Programming using JavaScript. Don’t let the name deceive you. The book even acknowledges the level of “academic absurdity” you can get into with functional programming. You can even get away with only reading the first part to really get a thoroughly practical understanding of functional programming.

Before getting into functional programming, I thought curry was a spice powder. Now I realize it’s a way of building a blockchain of functions with immutable state. Mind blown! Currying and immutability are the two biggest strengths of functional programming, and I wanted a practical way to use them in everyday application code. Functional programming was still too theoretical for me, but now I could at least see where I was using it and recognize its patterns. My eyes had been opened. I felt like I had awakened and saw the matrix. I was already using functional programming. It was Everywhere!

Where does Reactive come into Functional Programming?

Before getting into examples of famous libraries that use functional programming, it’s worth mentioning that one of the best use cases of functional programming is within the context of a reactive environment with asynchronous data flows like User Interfaces and web servers. These areas are also where a good chunk of professional programmers are employed now. If we can reduce bugs in our code with functional programming and ship better, more usable software, then a few hours of learning is worth giving our users a better experience.

On the User Interface side, several popular JavaScript libraries already use the ideas of functional programming. In Redux, for example, state is stored as a single immutable object. The reducer, responsible for calculating state, is the most innovative part about Redux. Its creator named the entire library on it. Immutability is enforced in functional programming and helps solve a huge chunk of bugs related to asynchronous data. When we combine functional programming within a reactive environment like a User Interface, the programming pundits start calling it Functional Reactive Programming (FRP).

Another functional JavaScript library that was popularized through Angular is RxJS, or more generally ReactiveX. Unlike Redux, which is a programming methodology, ReactiveX is a fully featured reactive programming library that became more functional over the years. The best part is that it is now cross-platform and available in almost all languages. It also has a similar naming convention to even the most die-hard functional programming libraries in JavaScript like Ramda (map, reduce, filter, etc.).

Image for post
Image for post
So reduce. Very map. All filter.

A Friendly and Practical functional programming ecosystem that is truly Cross-Platform

Since I came across RxJS through Angular, but also program with React, the last two versions(v5 and v6) were very interesting to me because they let RxJS become cross-platform within the JavaScript world. I could now use it everywhere. They also separated out their most powerful operators (essentially pure functions) into their own folder. This makes you more aware of what each function does and gives us developers a very powerful functional programming toolkit. All of these operators are “pipeable” and can be “curried” together to form higher order functions that are highly reusable.

Although learning to use RxJS effectively is by no means easy, one of the most motivational things that kept me going was the colorful stream diagrams and the wealth of resources available for learning it. It was almost as though the community was saying, “Hey we know this is hard, but colorful pictures! Data is just a stream!” Then things started to make sense, and my programming became much more predictable. Thinking of data as a stream that you route with functions like map and filter changes your programming style. You organize your files more deliberately. You can start writing shorter blocks of code that do much more, like one-line loops that are actually readable with ‘map’.

With an FRP library like ReactiveX, we can use the comprehensive tool chest to build our own custom functions. We can start recording the programming patterns we use in our code to handle data and reuse them anywhere we need to. It helps us visualize data for what it really is, free from each language’s arbitrary constructs. It helps us see the matrix.

Written by

Renaissance Engineer. Entrepreneur. Passionate about technology, education, and the environment.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store