Skip to content

Instantly share code, notes, and snippets.

View bhaveshdaswani93's full-sized avatar
๐Ÿ‘‹
Building Awesome things with Laravel React Vue

Bhavesh Daswani bhaveshdaswani93

๐Ÿ‘‹
Building Awesome things with Laravel React Vue
View GitHub Profile
@bhaveshdaswani93
bhaveshdaswani93 / compose_and_pipe.js
Last active December 29, 2019 13:14
Let's understand compose and pipe with an example
// Our task is to multiply a number with 3 and take absolute of that number
const multiplyWith3 = (x)=>x*3;
const getAbsouleOfNum = (x)=>Math.abs(x)
// here we have two component multiplyWith3 and getAbsouleOfNum
// if we want to do that without compose we will do like these
let x = 15;
let xAfterMultiply = multiplyWith3(x);
let xAfterAbsoulte = getAbsouleOfNum(xAfterMultiply);
//Let's do in composable way
@bhaveshdaswani93
bhaveshdaswani93 / memoization_in_javascript.js
Created December 28, 2019 07:52
In this gist i will try to explain memoization in javascript
const notMemoized = (x) => {
return x*2;
}
notMemoized(2); // 4
notMemoized(2); // 4
// every time we run the function the multiplication code get executed which can be optimized by memoization
const memoizedFn = () => {
const cache = {};
@bhaveshdaswani93
bhaveshdaswani93 / partial_application_in_js.js
Created December 28, 2019 05:15
In this gist i will try to explain you about partial application
const multiply = (a,b,c) =>a*b*c;
//Partial Application
const partiallyMultiplyBy5 = multiply.bind(null,5);
partiallyMultiplyBy5(4,10) //200
// in above example partiallyMultiplyBy5 partially apply multiply function with 5 as first argument.
// when executing the partiallyMultiplyBy5 function we just have to pass remaining paramater as the first argument 5 has been remember
// due to closure.
@bhaveshdaswani93
bhaveshdaswani93 / curring_javascript.js
Created December 28, 2019 04:39
Currying in javascript
const multiply = (a,b) => a*b;
multiply(4,6) // 24
//above is the function that take multiple argument let convert into curring
const curring = (a)=>(b)=>a*b;
curring(5)(4); // 20
// we have converted multiply function that take 2 param
// into two function that takes one parameter at a time
@bhaveshdaswani93
bhaveshdaswani93 / high_order_function.js
Created December 28, 2019 03:11
In this example i will explain high order function
const hocFn = function() {
return function() {
return 5;
}
}
hocFn() // will return function
hocFn()() // will return 5
// hocFn return function so it is HOC
const fn = function(x) {
@bhaveshdaswani93
bhaveshdaswani93 / immutability.js
Created December 27, 2019 04:02
In this example i will try to explain immutability
let mutateObj = {
first_name:'lorem',
last_name:'ipsum'
};
let immutateObj = {
first_name:'irum',
last_name:'egestas'
};
@bhaveshdaswani93
bhaveshdaswani93 / imperative_vs_declarative.js
Created December 26, 2019 03:36
Imperative vs Declarative with example. Here i want you to understand what exactly these terms are
//Imperative: what to do and how to do
//Declarative: what to do and what should be done
// Task i want to loop through 1 to 5 and print in the console let see how we can do the code impertively as well as declaratively
//Imerative
for(i=1;i<=5;i++) {
console.log(i);
}
/*
@bhaveshdaswani93
bhaveshdaswani93 / idempotence.js
Last active December 30, 2019 03:40
Let's understand a key term idempotence in functional programming
//here i will try to explain a key concept idempotence with example
function notIdempotenceFn(num) {
return Math.random(num);
}
notIdempotenceFn(5);
notIdempotenceFn(5);
// the output of above two function will be differeht however passing the same input so the function is not idempotent
function idempotentFn(num) {
@bhaveshdaswani93
bhaveshdaswani93 / referential_transparency.js
Created December 24, 2019 03:36
Understand Referential transparency with example
let a = (num1,num2) => {
return num1+num2;
}
let b = (num) => {
return num*2;
}
console.log(b(a(3,4))) //output will be 14
// here i can replace a(3,4) expression with value 7 value and this will not effect to the result of the program because its return
@bhaveshdaswani93
bhaveshdaswani93 / pure_function_same_input_same_output.js
Created December 22, 2019 12:37
Pure function should qualify with if there is same input the output should be same no matter how many times it is called
function addTwoNumber(num1,num2) {
return num1 + num2;
}
addTwoNmber(3,4); // will return 7 no matter how many times is called
function multiplyWithCurrentTime(num) {
return num * new Date().getTime();
}