23 Aug 2017

Functional Programming in javascript

2:31 PM JavaScript 0 Comment

What is Functional Programming?

This question can be answered in many ways, let’s start with the basics of functional programming.

  • The “Functional Programming” is a one of the “Programming Paradigm”. whereas some other programming paradigms are “Imperative programming” and “Object Oriented Programming”.
  • Functional Programming is a paradigm where functions are kings, it is one of the ways in which we can approach any task or the problem.
  • Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects.
  • FP is declarative rather than Imperative and application state flows through pure functions.
  • Functional code tends to be more concise, more predictable and easy to understand than the imperative or oop.

There are a lot of people and libraries out there to help you out with the functional programming.

The idea of functional programming is to express everything in terms of functions.And a function is, of course, a function that takes an input and gives an output.

So, we have to think about how the data flows through the various functions rather than thinking about how the objects interact.

Let’s see the following example which is non-functional

So, if we look at the above code, we can say that it is the imperative style of programming where we execute step by step.We have no functions in the above code.

Let’s see the functional way of doing this

So, there is a more functional way of programming where we have a function which takes an input and return’s a string with the name and message.

The functional programming avoids the side effects by using the functional programming.

So, what do we mean by side effects, well if a function takes an input and returns the output which is not according to the input.

If any functions which are taking anything from outside the function and involving it inside the function that is not the pure functions.

The idea of pure functions is it should return the same output for every same input. For instance, if u pass 2 and 3 in function for doing sum it should return 5.

Let’s see the below example which is not a pure function.

In the example, as we can see that we are involving a global variable called “name” inside the function, it should not be considered as a pure function.

So, how the pure function for above code looks like let’s see the below code :

The function returns the output based on the input we have passed.

We can also have Higher order functions where we pass one function to another and so on.

Let’s see the below example :

The above function returns another function and both the functions are taking one input parameters each and returning the full name.

These type of functions are called Higher order functions where one functions can return one or more functions.

Avoiding Iterations:

So, other things which we are going to avoid are the iterations like for, if etc., we will using something like map, reduce, filter kind of terminology.

The idea here is we have a list of vegetables and we have to transform it in some or other way and make a sandwich.

Let’s see we have vegetables and we have a slice functions where we are slicing all the vegetables which we can apply to all list of items.

Here we are mapping all the list items and let’s say we have a chop functions where we are slicing all the vegetables and returning back from chop function.

Then, we have something called reduce where it will combine all the chopped items layer by layer and make a sandwich.

We also have a filter function, it comes into play when let’s say u hate cucumber and u don’t want it into your sandwich so the filter will help u to prepare your sandwich without cucumber.

So, the above examples show how we transform our list using map, reduce, filter technique.

Avoiding Mutability:

These are the another concept where we avoid mutation of data.

Mutation will be considered as a non-functional, let’s see the below example

So, these are called mutation, here we are doing modification in the original list.

If we modify the original list it will cause lot issues or bugs, the reason is the above array might have used at multiple places in real time programming.

This is one of the most important reasons why functional programming avoids mutation in the data.

So, instead of changing the original array how to add the room D? let’s see

Instead of modifying the original data we have created a new array and added a room called ‘D’ in a newRooms array.This is the one of avoiding the data mutation.

So, if we look at the above example we can see that copying array is not the solution for our problem it will cause a lot of efficiency issues in the real time.

we cannot go and copy the array every time, processing array is not the solution, it will cause a lot of memory wastage and we have to process all the elements in the list.

One way of avoiding the above issue is by using the persistent data structures.

Persistent Data Structures to avoid Mutability :

In order to use persistent data structures, we have to use some of the libraries like Mori, Immutablejs etc.

These two libraries are well known so we can proceed with any one as you will feel comfortable.

So, the next question comes in mind when we talk about Persistent DS is how they really work.

Let’s say we have the following array

The libraries which we have mentioned above comes with all help functions like map, reduce etc.

Let’s take another example, using the Persistent Data Structures.

So, this is all about functional programming in javascript, if you have any doubts or suggestions please comment it in the comment box.Thanks for reading this Blog.

Leave a Reply