Functional Programming with JS

Functional programming is a programming paradigm designed to handle pure mathematical functions. This paradigm is totally focused on writing more compounded and pure functions.

Functional programming is a particular kind of declarative programming.

F…


This content originally appeared on DEV Community and was authored by Mahmoud Hassan

Functional programming is a programming paradigm designed to handle pure mathematical functions. This paradigm is totally focused on writing more compounded and pure functions.

Functional programming is a particular kind of declarative programming.

First, you need to know the difference between declarative programming and imperative programming, they are js paradigms or techniques to organize your code.
In imperative programming, we specify the program logic describing the flow control.

let name = "Ali";
let Greeting = "Hi, ";
console.log(Greeting , name);  // Hi,  Ali

In the opposite, declarative programming we specify the program logic without describing the flow control

const Greeting = (name) => {
    console.log('Hi, ',name);
}

Greeting("Ali"); // Hi, Ali

So, as you have noticed, the functional programming focuses on the code being clean, organized, and reused through

  1. Pure Functions: are simple and reusable. They are completely independent of the outside state(global variables), easy to refactor, test and debug. A pure function is a function which given the same input, will always return the same output.
const add = (x,y) => {
    console.log(x+y);
}
add(5,4) // 9

Math.random is a popular example of not pure function.
another example for not pure function:

let count = 0;
const incCount = (value) => count += value;
  1. Higher-Order Functions: they can receive a function as a parameter(callback) and also can return a function, they are very helpful for writing complex functions.
const animals = ["Cat", "Dog", "Elephant",
 "Giraffe", "Lion", "Monkey"];
const zooWithoutCat = animals.filter(animal => animal !== "Cat");

Note ==> Don't Iterate you can use higher-order functions like map, filter, reduce, find...

let numbers = [2, 4, 6];
let numbersX2 = numbers.map(number => number*2); // [ 4, 8, 12 ]
  1. Avoid Mutability: you must avoid changing the data.
let num1 = [1, 2, 3];
let num2 = num1;

any change in num2 affects num1 (mutability), we can fix this problem by using higher-order functions or spread operator.

let num2 = [...num1];
  1. Persistent Data Structures for Efficient Immutability

Think of all data as immutable, never changing.

the problem with immutability is that you need to copy all data for a little change and this can give you efficiency problems, because you will use a lot of space, so What is the solution?
Don't Worry
there are many of js libraries that handle this problem like:

  • Mori
  • Immutable.js
  • Underscore
  • Lodash
  • Ramda they depend on structural sharing idea Image description Note that the yellow squares are shared between 2 variables.

Thanks for your time
you can add me on LinkedIn: Link


This content originally appeared on DEV Community and was authored by Mahmoud Hassan


Print Share Comment Cite Upload Translate Updates
APA

Mahmoud Hassan | Sciencx (2021-10-18T19:26:17+00:00) Functional Programming with JS. Retrieved from https://www.scien.cx/2021/10/18/functional-programming-with-js/

MLA
" » Functional Programming with JS." Mahmoud Hassan | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/functional-programming-with-js/
HARVARD
Mahmoud Hassan | Sciencx Monday October 18, 2021 » Functional Programming with JS., viewed ,<https://www.scien.cx/2021/10/18/functional-programming-with-js/>
VANCOUVER
Mahmoud Hassan | Sciencx - » Functional Programming with JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/18/functional-programming-with-js/
CHICAGO
" » Functional Programming with JS." Mahmoud Hassan | Sciencx - Accessed . https://www.scien.cx/2021/10/18/functional-programming-with-js/
IEEE
" » Functional Programming with JS." Mahmoud Hassan | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/functional-programming-with-js/. [Accessed: ]
rf:citation
» Functional Programming with JS | Mahmoud Hassan | Sciencx | https://www.scien.cx/2021/10/18/functional-programming-with-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.