What is Adavnced function [ CCC ] Compose, Closures, Currying
What are Closures?
There was an execution of the function. The function will not be executed again. However, it will remember that there are references to those variables, so the child scope always has access to the parent scope.Examples:
const first = () => {
const greet = 'Hey! Pandeyji';
const second = () => {
alert(greet)
}
return second;
}
const newFunc = first();
newFunc();
Output: Show Pop-Up -> Hey Pandeyji
What is Currying ?
The process of converting a function that takes a number of arguments into a function that only takes one argument at a time.
Example:
const curriedMultiply = (a) => (b) => a * b;
const multiplyBy5 = curriedMultiply(5)
multiplyBy5(5);
multiplyBy5(10);
multiplyBy5(11);
Output:
25(5*5)
50(5*10)
55(5*11)
What is Compose?
The act of combining two functions to generate a third.
Example:
const compose = (f,g) => (a) => f(g(a));
const sum = (num) => num + 1;
compose(sum,sum)(5)
Output: 7
//(5+1 =6, 6+1 =7)
No comments