Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 18, 2021 07:26 pm GMT

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.

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
  • Ramdathey depend on structural sharing idea Image descriptionNote that the yellow squares are shared between 2 variables.

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


Original Link: https://dev.to/trezeguit/functional-programming-with-js-1bgd

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To