Composition in Javascript | Javascript Interview Questions
HTML-код
- Опубликовано: 5 фев 2025
- Closure in Javascript: • Closure in Javascript
Currying in Javascript: • Currying in Javascript...
In this video, we'll explore the concept of composition function in Javascript.
Connect With Me: bio.link/piyus...
More Playlists
► Complete ReactJS Tutorial Series - • Complete React Tutoria...
► Complete Firebase & React Tutorial Series - • Firebase with Reactjs ...
Social Links
► Twitter - / piyushgarg_dev
► LinkedIn - / piyushgarg195
Video Titles
What is currying in Javascript?
Javascript Composition
Javascript Composition Functions
Javascript Interview Questions
Hashtags
#javascript #closure #currying #javascriptcurry #webdev #webdeveloper #javascriptinhindi #javascriptinterviewquestions #webdevelopment #javascriptinhindi
Key takeaways from the video:-
To compose two functions accepting two arguments, following can be done:-
function composeTwo(fn1, fn2){
return function(a, b){
return fn2(fn1(a,b));
};
}
In ES6, the same can be written as, const c2f=(fn1, fn2) => (a,b) => fn2(fn1(a,b));
and for composing unlimited functons, we can do the following:-
function compose(...fns){
return function(...values){
return fns.reduce( (a,b) => b(a), value);
};
}
leetcode par js ke problems solve kar raha tha function compositions se related question aaya youtube pe search kiya bhaiya ka video dikha aur pura concept samaz mein aagaya ab question solve karunga
👌😊
😏You Did Not Taught Us Well...
8:56 You Got Stuck...Where You Used reduceRight...
and After It You Cutted Your Video
And Solved By Making It "reduce" function... and you didn't even Mention About It...
This Is Where I Also Stuck And THinking Why Your Code Is Working And Mine Not.................................
At Least Smjana To Chye Tha Ki Kyu Erroor Aa rhi thi aur kyu reduceRight Function ki jgh reduce function ka use karna pda....
Sorry,,,But You Are Teaching Next Level,,,,But I Have Head Ache rn. so please Give Answer The My q's.... if possible 🙏🏼❤
const addition = (...number) => {
return number.reduce((acc, curr) => acc + curr, 0);
};
const subtraction = (...number) => {
return number.reduce((acc, curr) => acc - curr, 0);
};
const multiplication = (...number) => {
return number.reduce((acc, curr) => acc * curr, 1);
};
const division = (...number) => {
return number.reduce((acc, curr) => acc / curr, 1);
};
const square = (number) => {
return number * number;
};
const compose =
(...fns) =>
(...args) => {
return fns.reduceRight((acc, fn) => [fn(...acc)], args)[0];
};
const number = [1, 2];
const addAndSquare = compose(square, multiplication);
console.log(addAndSquare(...number));
Great Explanation , I mean Effortless, You are Natural bro
Thankyou bro 😇
Bhaiya you’re best the way you narrate these complex concepts in a story way hats off❤️
ohhh! I am just a beginner, who was following your series, but this is the only lecture which has gone total parabola over my head. Shit! JS is complex.......
Great Tutorial Thank U
Maza aagaya
hello ... I didnt get line number 21 ... you said b ke andar a ko wrap karna hai .... what is it .. could you please explain
ok I got it .. i was getting confused by assuming a and b as arguments (as in first half of the video both are referred as argument) ... but you are referring it as functions when you strated unlimited functions and arguments concept
Bhai Square function is supposed to square a number not multiply any two numbers!
sir unlimited composition ke liye console mai NaN show kar raha hai.....code mai koi galti nahi hai ....I checked
const addition = (...number) => {
return number.reduce((acc, curr) => acc + curr, 0);
};
const subtraction = (...number) => {
return number.reduce((acc, curr) => acc - curr, 0);
};
const multiplication = (...number) => {
return number.reduce((acc, curr) => acc * curr, 1);
};
const division = (...number) => {
return number.reduce((acc, curr) => acc / curr, 1);
};
const square = (number) => {
return number * number;
};
const compose =
(...fns) =>
(...args) => {
return fns.reduceRight((acc, fn) => [fn(...acc)], args)[0];
};
const number = [1, 2];
const addAndSquare = compose(square, multiplication);
console.log(addAndSquare(...number));
can't we write like this ? type Add = (a:number,b:number) => number
const add:Add = (a,b) => a+b
type Square = (a:number) => number
const square =(a) => a*a
const flow = (f,g) => (x,y) => f(g(x,y))
const add_then_square = flow(square,add)
console.log(add_then_square(2,4))
in functional programming first we should define the type of each method , each variable .
nope you're not just writing add and square all the time, it was just an example ,in real these composition technique can be used in hashing algos, string matching etc
try writing this your way,fn(double(poweroftwo(add(removeodds(...args)), this is currently written in python, but if i have to rewrite it in javascript i would definitely use composition.
normal devs won't encounter them , but those who write tools for devs use them.
thanks
❤
mai next js ka latest version ka use karke ek full stack e-commerce webapp ka tutorial banane ka soch raha hu iss video me html, css, javascript, node js , mongodb, redis, redux toolkit, cloudnery sabhi ka use kab, kyo, aur kaise ek sath use karna sikhenge aur iss app ko vercel ke server par free me deploy bhi karega agar mere comment me 200 likes hota hai to Mujhe RUclips par video banane ka motivation milega
waiting for your video bro😂
thanks