Functional Programming: Introduction

Functional Programming: Introduction

What is Functional Programming? A simple Introduction.

Assalam u Alaikum & Hello Everyone! 👋

I hope you all are good ✨🤗

I'm back with another series full of learning i.e Functional Programming 🚀

In this article, we are going to learn What is functional programming and what we are going to learn in this series.

Contents

  • âš¡ Imperative Programming
  • âš¡ Declarative Programming
  • âš¡ Imperative vs Declarative Programming.
  • âš¡ Now, What is Functional Programming.

Let's start 🚀

Functional Programming is another Programming Paradigm just like Object-Oriented Programming. There are many small bits and pieces which we have to understand before understanding what is functional programming. The most important concept that is the prerequisite to understanding FP, is the understanding of Imperative & Declarative Programming

Imperative Programming

Imperative programming is a programming paradigm in which the program is written in a structured manner. Imperative programming describes the computer "How" to accomplish a specific task.

Procedural & OOP both paradigms are of Imperative Programming. We explicitly tell how we want to accomplish a specific task.

Example

Suppose that, we have a scenario in which we have to write a function that accepts an array of integers and returns all the integers that are less than 10.

Here is a way to solve this problem in an Imperative way:

const filterLessThan10 = (arr) => {
  // 1. create a new array
  const result = [];
  // 2. loop over tha array
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < 10) {
      // 3. if the item is less than 10, push it into result
      result.push(arr[i]);
    }
  }
  // 4. return result
  return result;
};

const numbers = [1, 2, 10, 42, 4, 98, 23, 92, 9, 2, 7];
console.log(filterLessThan10(numbers)); // [1, 2, 4, 9, 2, 7]

As you can see, in this program, we are explicitly telling the steps that a computer has to perform. In technical terms, there is No Abstraction in the function filterLessThan10. We are completely aware of how the function works internally.

Declarative Programming

Declarative programming is a programming paradigm in which the programmer defines what needs to be accomplished by the program without defining how it needs to be implemented.

In simple words, we don't tell the computer how to achieve a task, instead, we just command the computer to perform this task with zero interest in how the computer will perform it.

Prolog, SQL, and embedded SQL are some of the well-known examples of declarative programming languages.

Example:

We will refactor our filterLessThan10 and write it in a declarative manner.

const filterLessThan10 = (arr) => {
  return arr.filter((item) => item < 10);
};

const numbers = [1, 2, 10, 42, 4, 98, 23, 92, 9, 2, 7];
console.log(filterLessThan10(numbers)); // [1, 2, 4, 9, 2, 7]

As you can see, we have called the built-in filter method. We don't know how the filter is implemented. In technical terms, the filter method is an Abstraction and we are hiding all the internal details. We are just providing the filter criteria without the looping, pushing, and returning details.

Declarative vs Imperative Programming

Imperative ProgrammingDeclarative programming
It uses statements that change a program’s state.It expresses the logic of a computation without describing its control flow.
Imperative Programming is like your friend calling your father and telling him how to fix your car step by step.Declarative Programming is like asking your friend to fix your car. You don’t care how to fix it, that’s up to him.
We do not hide the details of a task how it's performed (Though we can use Abstraction which is an OOP principle)By Default Abstraction, The internal details are hidden among the tasks.

Now, What Is Functional Programming?

carbon.png

Functional Programming is a programming paradigm that follows the declarative paradigm approaches to perform computational tasks. In functional programming, the programming is done with expressions or declarations instead of statements.

In functional programming, we break a big task into many chunks (pure functions) and perform the tasks by calling them accordingly. The term functional programming has been derived from the mathematical functions.

In mathematics, we often see something like:f(x) = y which means a function f will take an input x and will produce some result y. The idea is the same in the world of functional programming as well.

Keeping all this in mind, we can describe functional programming as:

Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects.

What We Will Learn?

These are the topics which we are going to learn in this series in detail. Every topic will contain multiple small exercises and real-world use cases and by the end of it, you'll be confident in functional programming.

  • Advantages & Applications of FP
  • Immutability
  • Pure Functions & Side Effects
  • Higher-Order Functions
  • Function Composition

That's it, folks! hope it was a good read for you. Thank you! ✨

👉 Follow me: Github Twitter LinkedIn Youtube

Â