Currying is the process of converting a function that takes multiple arguments at once into a sequence of function's each with a single argument.
๐๐ผ๐ ๐ฑ๐ผ๐ฒ๐ ๐ถ๐ ๐๐ผ๐ฟ๐ธ?
Let's say you have a function with multiple arguments: sum(a,b,c).
๐จ๐๐ถ๐ป๐ด ๐๐๐ฟ๐ฟ๐๐ถ๐ป๐ด, ๐๐ผ๐ ๐๐ถ๐น๐น ๐ด๐ฒ๐ ๐ฎ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป ๐๐๐บ๐ฌ(๐ฎ) ๐๐ต๐ฎ๐ ๐ฟ๐ฒ๐๐๐ฟ๐ป๐ ๐ฎ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป ๐๐๐บ๐ญ(๐ฏ) ๐๐ต๐ฎ๐ ๐ฟ๐ฒ๐๐๐ฟ๐ป๐ ๐ฎ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป ๐๐๐บ๐ฎ(๐ฐ).
๐๐ป ๐ฆ๐ถ๐บ๐ฝ๐น๐ฒ ๐ง๐ฒ๐ฟ๐บ๐: ๐๐๐บ(๐ฎ, ๐ฏ, ๐ฐ) โ> ๐๐๐บ๐ฌ(๐ฎ) => ๐๐๐บ๐ญ(๐ฏ) => ๐๐๐บ๐ฎ(๐ฐ)
๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ ๐
const sum = (a) => {
return (b) => {
return (c) => {
return a + b + c;
};
};
};
const sum = (a) => (b) => (c) => a + b + c;
๐๐ผ๐ ๐๐ผ ๐ฐ๐ฎ๐น๐น ๐๐ต๐ฒ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป?
๐ฌ) sum(10)(20)(30); // 60
๐ญ) const addTen = sum(5)(5);
addTen(20); // 30