Sunday, 9 December, 2018 UTC


Summary

This is my first blog for the series called “Just so you know” where I will talk about things that you might already know or not. So for now we going to talk about Functional Programming.
 
 
What is Functional Programming?
a programming paradigm just like OOP where you have objects with methods in them, Structured Programming, Procedural and more however Functional Programming is where functions is the main character. Basically functional programming provides pure function and avoids things like mutable data or side effects. so most likely the data enters as an input and the function will provide an output without affecting any variables.
 
 
Why Use Functional Programming?
  1. Easier to understand.
  2. Easier to debug.
  3. Reduce duplicate codes.
  4. Easier to test using testing scripts.
  5. Easier to predict its output.
  6. Avoid unwanted variable value changes.
 
So know we know the basic meaning of Functional Programming. Let’s see an example implementation.

Not Functional

const name = 'Rienz Otiong'; 
const greet = `Hi! ${name}, Welcome to Codedam`;

console.log(greet); // returns Hi! Rienz Otiong, Welcome to Codedam

Functional

const name = 'Rienz Otiong'; 
const greet = str => `Hi! ${str}, Welcome to Codedam`;

console.log(greet(name)); // returns Hi! Rienz Otiong, Welcome to Codedam
 
 
Pure Function
Functional Programming takes an input and use only that given input to provide an output which produces no side effects.
Side Effects  is an effect where it uses an external variable that is not within function or calling an external process like triggering a file edit or printing on the display. Pure Functions will make you avoid any unexpected output or unwanted process.
 

Not Functional (Impure Function)

let name = 'Rienz Otiong'; 
const greet = () => `Hi! ${name}, Welcome to Codedam`;

Functional (Pure Function)

let name = 'Rienz Otiong'; 
const greet = str => `Hi! ${str}, Welcome to Codedam`;
 
 
Higher Order Function
Higher Order Function is a function within a function or can take functions as inputs and/or output.
const addition = num1 => num2 => num1 + num2;
const addFour = addition(4);

console.log(addFour(8)); // returns 12

const fourPlusTwo = addition(4)(2);

console.log(fourPlusTwo); // returns 6
is same as
const addition = function(num1) {
    return function(num2) {
        return num1 + num2;
    }
};

const addFour = addition(4);

console.log(addFour(8)); // returns 12

const fourPlusTwo = addition(4)(2);

console.log(fourPlusTwo); // returns 6
the old way
 
 
Functional Methods (map, reduce, filter)
We might iterate an array using for loops and while loop however in a functional way to do it is use map, reduce and filter which returns a new array and doesn’t modify the original array.
So first map transform each item of the array which returns the same length of items. For example
const arr = [1, 2, 3, 4, 5];
const double = arr.map(item => item * 2);

console.log(arr); // [1, 2, 3, 4, 5]
console.log(double); // [ 2, 4, 6, 8, 10 ]
reduce on the other hand returns a data using all of the items resulting a single output value.
const arr = [1, 2, 3, 4, 5];
const sumArr = arr.reduce((acc, cur) => acc + cur);

console.log(arr); // [1, 2, 3, 4, 5]
console.log(sumArr); // 15
lastly filter where it checks every item and removes all of the items that failed on the given condition.
const arr = [1, 2, 3, 4, 5];
const arrEven = arr.filter(item => item % 2 === 0);

console.log(arr); // [1, 2, 3, 4, 5]
console.log(arrEven); // [ 2, 4 ]
 
 
Immutable Data
This is a continuation of Functional Methods on why we need to use it. Immutable Data is a data that can’t be change which enables us to debug easier since your confident that none of your methods or functions will change the value of your data. This avoids the fact that sometimes we don’t know where our data changes its value since numerous functions use and transform the value of that variable.

Mutable Data

const arr = [1, 2, 3, 4, 5];
arr[0] = 2;

console.log(arr); // [ 2, 2, 3, 4, 5 ]

Immutable Data

const arr = [1, 2, 3, 4, 5];
const newArr = arr.map(item => item === 1 ? 2 : item);

console.log(arr); // [ 1, 2, 3, 4, 5 ]
console.log(newArr); // [ 2, 2, 3, 4, 5 ]
 
 

For more “Just so you know” posts. Just click here

The post Just so you know: Functional Programming appeared first on Codedam.