Functional Programming : The need for the hour

Nikhil Goel
4 min readOct 30, 2020

In this growing world, everyday a new concept or new algorithm is introduced in Computer Science and that’s the beauty of this which makes this field interesting and exploring.

Each one of us have heard different types of programming like OOPs (Object Oriented Programming), Procedural Programming and Scripting Programming. There are many programming languages which supports these types of programming like C,C++,Java,C#,Ruby,Python,JS etc.

Today, I am going to discuss about a new type of programming i.e. Functional Programming.

This type of programming is now a days a very common practice. The basic idea behind this type of programming is mathematics. Although we all know the basic behind computer science is mathematics.

This type of programming takes the mathematics to a new level.

Just like mathematical functions, we just look at input and produce the output, there is no change in value i.e. no side effects. This is the general idea behind this type of programming. Let’s explore more about it.

First let’s try to understand with the help of mathematical example what it tries to implies.

Suppose I have a set of some characters,

let’s suppose it x.

x = [‘a’,’b’,’c’]

Now I have another function y that uses the values of x.

y = a(x).

Now, after executing the y. does the value of x changes??

The answer would be No. because the y is using the values of x not manipulating the x.

So, this is the idea of functional programming, we are providing some inputs and gets output using the inputs but not changing the inputs.

Now, the question arises how we can implement such concept in our programming language.

Don’t worry it’s quite simple to do so. Now, let’s talk about it in detail.

Each one of us knows about data structures, these are the building block of a programming language as well as an application. So, first approach to the implementation of functional programming is making the data structure immutable. Now, these can’t be changed. Now, if anybody wants to change any value, he/she has to make a copy of this data structure and use them. In this way the input remains same even after performing different operations by the users.

Now, we wonder if we have to copy the whole data structures each time, this leads to the problem of memory wastage. So, to solve this type of problem we have something called persistent data structures.

Persistent data structures are immutable which are implemented like a tree structure to store values, so you don’t have to copy whole array each time, just copy that node of data which you required to manipulate. Generally, people use the concept of 32 wide nodes. In this each node has 32 another nodes which can store millions of data.

So, now list out some advantages of this type of programming.

1. Easy to think: Since the input remains same and you don’t have to worry about what’s the input is. It’s quite easy to think how to solve various programming problem.

2. Easy to understand: Now, the function we are implementing has no side effects, so it can be easily understood by anyone.

Each type of programming has advantages as well as disadvantages same goes with functional programming as well.

Now, let’s explore some of the problems and how we can solve them.

Our customers really don’t care about how we implemented a solution to fix a problem they care about the side effects the functions are producing like updating the value in database, creating a file etc.

As we know functional programming doesn’t have any side effects, so it’s lead to a problem for our customer.

Don’t worry we also have a solution for this problem as well. To solve this problem we are going to use bridge pattern i.e. we need a bridge between functional programming and object oriented programming.

We can achieve this with the help of closure state. It has something called atoms which has a container for mutable state.Through the function at atom, we evaluates the value of the function and it becomes the value of the atom and this is the bridge between the two.

Suppose, if two functions hits the atom to update the value at the same time, both functions will have the same initial value, but one function might be faster than other which will change the value of atom, now when second function tries to change the value of atom, it detects the change in value and again return the function but this time with the new value and this is how the collision is handled same as done in the database transaction.

So, In functional programming we don’t have any side effects except the updating the atom and we are controlling the value of atom as it is the bridge between our lovely functional programming world and outside messy world that has side effects. In this type of programming instead of changing the value of data directly ww throw a function to do so. We have a queue to maintain the record of side effects we are going to do like deleting a file record, write a file etc.

Surely, this type of programming is going to become very popular in the future time as it is easy to understand and with no side effects it will completely change the way of thinking and implementing the ideas to solve the real world problem

--

--