Introduction to Functional Programming

Vaibhav Mahalle
3 min readFeb 16, 2022

--

Whenever there’s a discussion going on about which Programming Paradigm is better and you are standing there like…

So let’s try and change that….

  1. What is Functional Programming anyway?

Functional programming is a programming style where programs are constructed by applying and composing functions. It is a declarative programming paradigm.

Functions are first-class citizens in functional programming — they can be used as variables, passed as parameters to other functions, and returned from other functions.

2. Why Functional Programming?

Functional programming came about because as the scale of the application grows maintaining the code becomes quite tedious.

FP abstracts the most redundant code using and reusing the functions and functional code is easier to scale and maintain compared to other programming paradigms.

You can read about the benefits of functional programming here.

3. How to do functional programming?

  • Pure functions
  • Higher-Order functions
  • Don’t iterate using for or while use map, filter, reduce.
  • use immutable variables

Do everything using functions:

Let’s say you want to print a statement: ‘XYZ is my favorite cricketer’, where XYZ would contain the name of the user’s favorite cricketer.

//Imperative style in javascriptlet name = "M.S.Dhoni";
let statement = "is my favorite cricketer";
console.log(name+" "+statement);
//Using functions in jslet myFavoritePlayer = function(name){ return `${name} is my favorite player` ;}console.log(myFavoritePlayer("M.S.Dhoni"));

Notice in the above code that while using the imperative style you are focusing on step by step approach of how to print your favorite player’s name. If you want to change your favorite player you will have to reassign the variable name or use a new variable.

But in the case of a function, you can reuse the entire function to print different results each time.

Pure functions:

the function return values are identical for identical arguments

the function application has no side effects

simply for the given input, you will get the same output no matter what, also the function doesn’t affect any other local or global variables. It simply must return output for a given input.

in the example above if we provide the name of the player function is adding that name and returning the output. It has no side effects that’s why the function is pure.

2. Higher-Order Functions:

a higher-order function is a function that does at least one of the following:

we can pass functions as arguments to the higher-order functions.

3. Map filter and reduce:

We all have used the iterative for loops to iterate over a simple array of numbers to print them. But you could use the map function to iterate over the array.

suppose you want to multiply all the numbers in an array.

const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)

function myFunction(num) {
return num * 10;
}

here you don't have to use the for loop and it makes everyone’s life easy. you can look up map, filter, and reduce in the links.

4. Use Immutability :

Immutability means that once the function variable or the variable is assigned any value then you should not change that value.

But why not?

Because it becomes very tough to track the changes in the variables and debugging the code becomes that much harder.

But if we assign a new variable each time we need a different value for a similar variable then the program might become slower and also take a lot more space.

For efficient immutability, we can use Persistent data structures

Functional programming is quite huge and has a lot more to explore. So this post is just a start in the understanding of the paradigm.

--

--

Vaibhav Mahalle

Hey there I am a programmer. I mostly write about the things I am learning.