๐
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let mutateObj = { | |
| first_name:'lorem', | |
| last_name:'ipsum' | |
| }; | |
| let immutateObj = { | |
| first_name:'irum', | |
| last_name:'egestas' | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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); | |
| } | |
| /* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |