Currying
key points
⦿ Currying is transformation of functions that translates a function from callable as f(x, y, z) into callable as f(x)(y)(z).
1) The first example is straight forward. We have 3 parameters to pass. The result is 56
⦿ Infinite currying - we can pass n number of parameters.
1) Initially a is 5
2) It returns function. Then we pass b whichs is 4. Because b exists. It satisfies the if condition, 20 is passed to infinitmul recursively. It returns function again.
3) Now b is 8. then if condition is satisfied. 160 is passed to the function recursively. Returns function
4) now there is no parameter. It goes to else block and it returns 160 as the result
1) The first example is straight forward. We have 3 parameters to pass. The result is 56
⦿ Infinite currying - we can pass n number of parameters.
1) Initially a is 5
2) It returns function. Then we pass b whichs is 4. Because b exists. It satisfies the if condition, 20 is passed to infinitmul recursively. It returns function again.
3) Now b is 8. then if condition is satisfied. 160 is passed to the function recursively. Returns function
4) now there is no parameter. It goes to else block and it returns 160 as the result
const multiply = (a, b) => a * b;
const curriedMultiply = (a) => (b) => (c) => a * b * c;
console.log(`%c ${multiply(4)}`, "color:red;font-size:20px");
console.log(`%c ${curriedMultiply(4)(7)(2)}`, "color:red;font-size:20px");
const infinitmul = (a) => {
 return (b) => {
  if (b) return infinitmul(`${a} * ${b}`);
  else {
   return a;
  }
 };
};
console.log(`%c ${infinitmul(5)(4)(8)()}`, "color:red;font-size:20px");
Functions are first class citizens
key points
⦿ function can be treated as variable
⦿ function can be passed as arguments
⦿ function can return functions
⦿ function can be passed as arguments
⦿ function can return functions
//**function can be treated as variable**
const calculator = () => 4 * 3;
let x = calculator();
//**function can be passed as arguments**
const printCallback = (callback) => {
 console.log(callback());
};
printCallback(calculator);
//**function can return functions**
function name() {
 return function (a) {
  console.log("Hello" + a);
 };
}
Higher Order Functions vs Callback functions
key points
⦿ Higher order functions are functions that takes other functions as arguments or return fuctions as arguments
⦿ Callback functions gets passed as an argument to another functions is called callback functions
⦿ Callback functions gets passed as an argument to another functions is called callback functions
- Example 1 -HOF
- javascriptLearning is returning function. so it is higher order function.
const javascriptLearning = (name) => {
 return function (topic) {
  console.log(`Hi ${name}. Are you learning ${topic} ??`);
 };
};
javascriptLearning("Anurag")("Javascript");
//output:- Hi Anurag. Are you learning Javascript ??
- Example 2 -HOF
- javascriptLearning is returning function. so it is higher order function.
- multiplication is callback function. Because this functions gets passed as an argument
- calculator is Higher order function – which takes function as arguments. It takes operator function as argument.
const addition = (x, y) => {
 return x + y;
};
const multiplication = (x, y) => {
 return x * y;
};
const calculator = (num1, num2, operator) => {
 return operator(num1, num2);
};
console.log(calculator(10, 5, multiplication))
//!OUTPUT -50
Code Refactor
key points
⦿ Chances of sending wrong value to the function is more in sendEmail
⦿ Refactor as refactoredSendEmail. Force the calling function to pass the parameters(to,body,subject). And destructure in the actual method (refactoredSendEmail)
⦿ Refactor as refactoredSendEmail. Force the calling function to pass the parameters(to,body,subject). And destructure in the actual method (refactoredSendEmail)
const sendEmail = (to, subject, body) => {
 if (to) {
  //send email
 } else {
  //some error message
 }
};
//chances of sending wrong value is more as below .
//we are sending email address in subject....
sendEmail("teaching js", "anurag.nayak@hotmail.com", "still novice");
//Refactored Code
const refactoredSendEmail = ({ to, subject, body }) => {
 if (!to) {
  //error message
 }
 //sendEmail
};
refactoredSendEmail({
 body: "improvement",
 to: "anurag.nayak@hotmail.com",
 subject: "still novice",
});
Code Refactor
& operator is O(1)
let n = 64;
//Solution :-1
while (n !== 1) {
let rem = n % 2;
if (rem === 0) {
n = n / 2;
if (n === 2) {
console.log("Number is power of 2");
break;
}
} else {
console.log("Number is not power of 2");
break;
}
}
let number1 = 32;
let number2 = 31;
console.log(number1.toString(2));
console.log(number2.toString(2));
//Solution :-2
let output = number2 & number1;
if (output === 0) console.log("Number is power of 2");
else console.log("Number is not power of 2");
// number1-32
// number2-31
// 100000
// * 011111
// --------
// 000000
// --------
// number1-34
// number2-33
// 100010
// * 100001
// --------
// 100000
// --------