๐ฏ ์ปค๋ง ๊ธฐ๋ฒ์ผ๋ก ํจ์ ์ฌ์ฌ์ฉ์ฑ ๋์ด๊ธฐ ๐

์๋ ํ์ธ์, ์ฌ๋ฌ๋ถ! ์ค๋์ JavaScript์ ๊ฟํ ์ค ํ๋์ธ '์ปค๋ง(Currying)' ๊ธฐ๋ฒ์ ๋ํด ์์๋ณผ ๊ฑฐ์์. ์ด ๊ธฐ๋ฒ์ ํ์ฉํ๋ฉด ํจ์์ ์ฌ์ฌ์ฉ์ฑ์ ์์ฒญ๋๊ฒ ๋์ผ ์ ์๋ต๋๋ค! ๐ ์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ฐ๋ ์ ๋ค์ด๋ณด์ จ์ ์๋ ์๊ฒ ๋ค์.
๊ทธ๋ผ ์ง๊ธ๋ถํฐ ์ปค๋ง์ ์ธ๊ณ๋ก ๋น ์ ธ๋ณผ๊น์? ์ค๋น๋์ จ๋์? 3, 2, 1... ์ถ๋ฐ! ๐
๐ก Tip: ์ปค๋ง์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ ๊ฐ๋ ์ค ํ๋๋ก, ๋ณต์กํ ํจ์๋ฅผ ๊ฐ๋จํ ํจ์๋ค์ ์กฐํฉ์ผ๋ก ๋ง๋๋ ๊ธฐ๋ฒ์ด์์. ์ดํดํ๊ธฐ ์ด๋ ค์ธ ์ ์์ง๋ง, ์ฐจ๊ทผ์ฐจ๊ทผ ์ค๋ช ๋๋ฆด ํ ๋ ๊ฑฑ์ ๋ง์ธ์!
๐ค ์ปค๋ง์ด ๋ญ๊ธธ๋?
์ปค๋ง์ด๋ผ... ๋ญ๊ฐ ๋ง์๋ ์์ ์ด๋ฆ ๊ฐ์ง ์๋์? ใ ใ ใ ํ์ง๋ง ์ฌ๊ธฐ์ ๋งํ๋ ์ปค๋ง์ ๋จน๋ ๊ฒ ์๋๋ผ ์ฝ๋ฉํ๋ ๊ฑฐ์์! ๐
์ปค๋ง์ ์ฌ๋ฌ ๊ฐ์ ์ธ์๋ฅผ ๊ฐ์ง ํจ์๋ฅผ ๋จ์ผ ์ธ์๋ฅผ ๊ฐ์ง ํจ์๋ค์ ์ฒด์ธ์ผ๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์ด์์. ์... ๋ญ ์๋ฆฌ๋๊ณ ์? ๊ฑฑ์ ๋ง์ธ์, ์์ ๋ฅผ ํตํด ์ฝ๊ฒ ์ค๋ช ํด๋๋ฆด๊ฒ์!
๐ ์์์ผ๋ก ๋น์ ํด๋ณด์๋ฉด: ์ปค๋ง์ ๋ง์น ์ฌ๋ฌ ๊ฐ์ง ์ฌ๋ฃ๋ฅผ ํ ๋ฒ์ ๋ฃ๊ณ ์๋ฆฌํ๋ ๋์ , ๊ฐ ์ฌ๋ฃ๋ฅผ ์ฐจ๋ก๋๋ก ๋ฃ์ด๊ฐ๋ฉฐ ์๋ฆฌํ๋ ๊ฒ๊ณผ ๋น์ทํด์. ์ด๋ ๊ฒ ํ๋ฉด ์ค๊ฐ ๊ณผ์ ์์ ๋ค์ํ ๋ง์ ๋ง๋ค์ด๋ผ ์ ์์ฃ !
๐ ์ปค๋ง์ ๊ธฐ๋ณธ ๊ฐ๋
์ผ๋ฐ์ ์ธ ํจ์๋ ์ด๋ ๊ฒ ์๊ฒผ์ด์:
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2, 3)); // 6
์ด๊ฑธ ์ปค๋ง์ผ๋ก ๋ฐ๊พธ๋ฉด ์ด๋ ๊ฒ ๋ฉ๋๋ค:
function curryAdd(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}
console.log(curryAdd(1)(2)(3)); // 6
์ด๋ค๊ฐ์? ๋ญ๊ฐ ๋ณต์กํด ๋ณด์ด์ฃ ? ใ ใ ใ ํ์ง๋ง ์ด๋ ๊ฒ ํ๋ฉด ์์ฒญ๋ ์ฅ์ ์ด ์๋ต๋๋ค!
๐ญ ์ปค๋ง์ ์ฅ์
- ํจ์๋ฅผ ์ฌ์ฌ์ฉํ๊ธฐ ์ฌ์์ ธ์.
- ๋ถ๋ถ ์ ์ฉ(Partial Application)์ด ๊ฐ๋ฅํด์ ธ์.
- ์ฝ๋๊ฐ ๋ ์ ์ฐํด์ง๊ณ ์กฐํฉํ๊ธฐ ์ฌ์์ ธ์.
์ด๊ฒ ๋์ฒด ๋ฌด์จ ๋ง์ธ์ง ๋ชจ๋ฅด๊ฒ ๋ค๊ณ ์? ๊ฑฑ์ ๋ง์ธ์! ์ง๊ธ๋ถํฐ ํ๋์ฉ ์์ธํ ์ค๋ช ํด๋๋ฆด๊ฒ์. ๐
๐ ์ปค๋ง์ ์ค์ ์ฌ์ฉ ์์
์, ์ด์ ์ปค๋ง์ ์ค์ ๋ก ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์์๋ณผ๊น์? ์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด๋ฐ ์์๊ฐ ๋์์ด ๋ ๊ฑฐ์์!
1. ๐ ์ ํธ๋ฆฌํฐ ํจ์ ๋ง๋ค๊ธฐ
๋จผ์ , ๊ฐ๋จํ ๊ณฑํ๊ธฐ ํจ์๋ฅผ ๋ง๋ค์ด๋ณผ๊ฒ์.
function multiply(a, b) {
return a * b;
}
console.log(multiply(2, 3)); // 6
console.log(multiply(2, 4)); // 8
console.log(multiply(2, 5)); // 10
์ด ํจ์๋ฅผ ์ปค๋ง์ผ๋ก ๋ฐ๊ฟ๋ณผ๊น์?
function curryMultiply(a) {
return function(b) {
return a * b;
}
}
const multiplyByTwo = curryMultiply(2);
console.log(multiplyByTwo(3)); // 6
console.log(multiplyByTwo(4)); // 8
console.log(multiplyByTwo(5)); // 10
์ด๋ค๊ฐ์? multiplyByTwo๋ผ๋ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฌ์ฉํ ์ ์๊ฒ ๋์ด์! ์ด๋ ๊ฒ ํ๋ฉด ์ฝ๋ ์ค๋ณต์ ์ค์ผ ์ ์๋ต๋๋ค. ๐
2. ๐จ ์คํ์ผ ํจ์ ๋ง๋ค๊ธฐ
์น ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด ์คํ์ผ์ ์์ฃผ ๋ณ๊ฒฝํด์ผ ํ ๋๊ฐ ์์ฃ . ์ปค๋ง์ ์ด์ฉํ๋ฉด ์ด๋ฐ ์์ ์ ํจ์ฌ ํธํ๊ฒ ํ ์ ์์ด์!
function setStyle(property) {
return function(value) {
return function(element) {
element.style[property] = value;
return element;
}
}
}
const setColor = setStyle('color');
const setBackgroundColor = setStyle('backgroundColor');
const setRedColor = setColor('red');
const setBlueBackground = setBackgroundColor('blue');
// ์ฌ์ฉ ์์
const div = document.createElement('div');
setRedColor(div);
setBlueBackground(div);
์์ฐ! ์ด๋ ๊ฒ ํ๋ฉด ์คํ์ผ ํจ์๋ฅผ ์์ฒญ ์ ์ฐํ๊ฒ ๋ง๋ค ์ ์์ด์. ์ฌ๋ฅ๋ท์์ ์น ๋์์ธ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ธฐ๋ฒ์ ์ ์ฉํจ์ ๋ฐ๋ก ์์์ฐจ๋ฆฌ์ค ๊ฑฐ์์! ๐
3. ๐ ์ธ์ฆ ํจ์ ๋ง๋ค๊ธฐ
์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ธ์ฆ์ ์ ๋ง ์ค์ํ์ฃ . ์ปค๋ง์ ์ด์ฉํ๋ฉด ์ธ์ฆ ๋ก์ง์ ๋ ๊น๋ํ๊ฒ ๋ง๋ค ์ ์์ด์.
function authenticate(username) {
return function(password) {
return function(role) {
// ์ค์ ๋ก๋ ์ฌ๊ธฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฒดํฌ ๋ฑ์ ๋ก์ง์ด ๋ค์ด๊ฐ ๊ฑฐ์์
console.log(`Authenticating ${username} with role ${role}`);
return username === 'admin' && password === '1234' && role === 'admin';
}
}
}
const authenticateAdmin = authenticate('admin')('1234')('admin');
console.log(authenticateAdmin); // true
const authenticateUser = authenticate('user')('5678')('user');
console.log(authenticateUser); // false
์ด๋ ๊ฒ ํ๋ฉด ์ธ์ฆ ๋ก์ง์ ๋จ๊ณ๋ณ๋ก ๋๋ ์ ์์ด์ ์ฝ๋ ๊ด๋ฆฌ๊ฐ ํจ์ฌ ์ฌ์์ ธ์. ๐จโ๐ป
๐ก Tip: ์ปค๋ง์ ์ฌ์ฉํ๋ฉด ํจ์์ ์ฌ์ฌ์ฉ์ฑ์ด ๋์์ง๊ณ , ์ฝ๋์ ๊ฐ๋ ์ฑ๋ ์ข์์ง ์ ์์ด์. ํ์ง๋ง ๋๋ฌด ๊ณผ๋ํ๊ฒ ์ฌ์ฉํ๋ฉด ์คํ๋ ค ์ฝ๋๊ฐ ๋ณต์กํด์ง ์ ์์ผ๋ ์ ์ ํ ์ฌ์ฉํ๋ ๊ฒ ์ค์ํด์!
๐ ์ปค๋ง์ ๊ณ ๊ธ ๊ธฐ๋ฒ
์, ์ด์ ๊ธฐ๋ณธ์ ์ธ ์ปค๋ง์ ๋ํด ์์๋ดค์ผ๋ ์กฐ๊ธ ๋ ๊ณ ๊ธ ๊ธฐ๋ฒ์ผ๋ก ๋ค์ด๊ฐ๋ณผ๊น์? ๐
1. ๐ ๋ถ๋ถ ์ ์ฉ (Partial Application)
๋ถ๋ถ ์ ์ฉ์ ์ปค๋ง์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ์ค ํ๋์์. ํจ์์ ์ผ๋ถ ์ธ์๋ง ๋ฏธ๋ฆฌ ์ฑ์๋๊ณ , ๋๋จธ์ง๋ ๋์ค์ ์ฑ์ธ ์ ์๊ฒ ํด์ฃผ์ฃ .
function partial(fn, ...args) {
return function(...moreArgs) {
return fn(...args, ...moreArgs);
}
}
function add(a, b, c) {
return a + b + c;
}
const add5 = partial(add, 5);
console.log(add5(10, 15)); // 30
const add5and10 = partial(add, 5, 10);
console.log(add5and10(15)); // 30
์ด๋ ๊ฒ ํ๋ฉด ํจ์์ ์ผ๋ถ ์ธ์๋ฅผ ๋ฏธ๋ฆฌ "๊ณ ์ "์์ผ๋๊ณ ์ฌ์ฉํ ์ ์์ด์. ์์ฒญ ํธ๋ฆฌํ์ฃ ? ๐
2. ๐ ํจ์ ์กฐํฉ (Function Composition)
์ปค๋ง์ ์ด์ฉํ๋ฉด ์ฌ๋ฌ ํจ์๋ฅผ ์กฐํฉํด์ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค ์ ์์ด์. ์ด๊ฑธ ํจ์ ์กฐํฉ์ด๋ผ๊ณ ํด์.
function compose(...fns) {
return function(x) {
return fns.reduceRight((v, f) => f(v), x);
}
}
const double = x => x * 2;
const increment = x => x + 1;
const square = x => x * x;
const doubleIncrementAndSquare = compose(square, increment, double);
console.log(doubleIncrementAndSquare(3)); // (3 * 2 + 1)^2 = 49
์! ์ด๋ ๊ฒ ํ๋ฉด ์ฌ๋ฌ ํจ์๋ฅผ ๋ง์น ๋ ๊ณ ๋ธ๋ก์ฒ๋ผ ์กฐ๋ฆฝํด์ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค ์ ์์ด์. ์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ธฐ๋ฒ์ ๊ฐ๋ ฅํจ์ ๋๋ผ์ค ์ ์์ ๊ฑฐ์์! ๐งฑ
3. ๐ญ ์ปค๋ง๊ณผ ํด๋ก์
์ปค๋ง์ ํด๋ก์ ์ ๋ฐ์ ํ ๊ด๋ จ์ด ์์ด์. ํด๋ก์ ๋ฅผ ์ด์ฉํด ์ปค๋ง ํจ์์ ์ํ๋ฅผ ์ ์งํ ์ ์์ฃ .
function counter() {
let count = 0;
return function(increment = 1) {
count += increment;
return count;
}
}
const myCounter = counter();
console.log(myCounter()); // 1
console.log(myCounter(2)); // 3
console.log(myCounter(5)); // 8
์ด ์์ ์์ myCounter ํจ์๋ ๋ด๋ถ ์ํ(count)๋ฅผ ์ ์งํ๋ฉด์ ํธ์ถ๋ ๋๋ง๋ค ๊ฐ์ ์ฆ๊ฐ์์ผ์. ์ด๋ฐ ์์ผ๋ก ์ปค๋ง๊ณผ ํด๋ก์ ๋ฅผ ์กฐํฉํ๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ํจ์๋ฅผ ๋ง๋ค ์ ์๋ต๋๋ค! ๐ช
๐ค ์ปค๋ง, ์ธ์ ์จ์ผ ํ ๊น?
์, ์ด์ ์ปค๋ง์ ๋ํด ๋ง์ด ์๊ฒ ๋์ฃ ? ๊ทธ๋ฐ๋ฐ ์ด๊ฑธ ์ธ์ ์จ์ผ ํ ์ง ๊ถ๊ธํ์ค ๊ฑฐ์์. ๊ฑฑ์ ๋ง์ธ์! ์ ๊ฐ ์๋ ค๋๋ฆด๊ฒ์. ๐
1. ๐ ์ค์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฐ ๋
์ ํ๋ฆฌ์ผ์ด์ ์์ ์ค์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฐ ๋ ์ปค๋ง์ด ์ ์ฉํด์.
function createConfig(baseConfig) {
return function(userConfig) {
return {...baseConfig, ...userConfig};
}
}
const createServerConfig = createConfig({host: 'localhost', port: 8080});
const devConfig = createServerConfig({env: 'development'});
console.log(devConfig); // {host: 'localhost', port: 8080, env: 'development'}
const prodConfig = createServerConfig({env: 'production', port: 80});
console.log(prodConfig); // {host: 'localhost', port: 80, env: 'production'}
์ด๋ ๊ฒ ํ๋ฉด ๊ธฐ๋ณธ ์ค์ ์ ๋ฐํ์ผ๋ก ๋ค์ํ ํ๊ฒฝ๋ณ ์ค์ ์ ์ฝ๊ฒ ๋ง๋ค ์ ์์ด์. ์ฌ๋ฅ๋ท์์ ์๋ฒ ๊ฐ๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ธฐ๋ฒ์ ์ ์ฉํจ์ ๋ฐ๋ก ์์์ฐจ๋ฆฌ์ค ๊ฑฐ์์! ๐ฅ๏ธ
2. ๐จ ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ง๋ค๊ธฐ
์น ๊ฐ๋ฐ์ ํ ๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๋ง๋ค ๋๋ ์ปค๋ง์ด ์ ์ฉํด์.
function handleEvent(eventType) {
return function(className) {
return function(callback) {
document.querySelector(className).addEventListener(eventType, callback);
}
}
}
const handleClick = handleEvent('click');
const handleHover = handleEvent('mouseover');
const handleButtonClick = handleClick('.button');
const handleLinkHover = handleHover('.link');
handleButtonClick(() => console.log('Button clicked!'));
handleLinkHover(() => console.log('Link hovered!'));
์์ฐ! ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ ๋ง ์ ์ฐํ๊ฒ ๋ง๋ค ์ ์์ด์. ๐จโ๐ป
3. ๐ ๊ฒ์ ๋ฐ ํํฐ๋ง ํจ์
๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๊ฑฐ๋ ํํฐ๋งํ ๋๋ ์ปค๋ง์ด ํฐ ๋์์ด ๋ผ์.
function filter(property) {
return function(value) {
return function(array) {
return array.filter(item => item[property] === value);
}
}
}
const filterByColor = filter('color');
const filterRed = filterByColor('red');
const items = [
{name: 'apple', color: 'red'},
{name: 'banana', color: 'yellow'},
{name: 'cherry', color: 'red'}
];
console.log(filterRed(items)); // [{name: 'apple', color: 'red'}, {name: 'cherry', color: 'red'}]
์ด๋ ๊ฒ ํ๋ฉด ๋ค์ํ ์์ฑ๊ณผ ๊ฐ์ผ๋ก ๋ฐฐ์ด์ ์ฝ๊ฒ ํํฐ๋งํ ์ ์์ด์. ๋ฐ์ดํฐ ์ฒ๋ฆฌ๊ฐ ๋ง์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ ๋ง ์ ์ฉํ๋ต๋๋ค! ๐
๐ Pro Tip: ์ปค๋ง์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ ๊ฐ๋ ์ค ํ๋์์. ์ฌ๋ฅ๋ท์์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณด์๋ฉด ์ด๋ฐ ๊ฐ๋ ๋ค์ ๋ ๊น์ด ์ดํดํ์ค ์ ์์ ๊ฑฐ์์!
๐งช ์ปค๋ง์ ์ค์ ์์ฉ
์, ์ด์ ์ปค๋ง์ ๊ธฐ๋ณธ ๊ฐ๋ ๊ณผ ์ฌ์ฉ๋ฒ์ ๋ํด ์์๋ดค์ผ๋, ์ค์ ํ๋ก์ ํธ์์ ์ด๋ป๊ฒ ํ์ฉํ ์ ์๋์ง ๋ ์์ธํ ์์๋ณผ๊น์? ๐ต๏ธโโ๏ธ
1. ๐ API ํธ์ถ ํจ์ ๋ง๋ค๊ธฐ
์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค๋ค ๋ณด๋ฉด API๋ฅผ ์์ฃผ ํธ์ถํ๊ฒ ๋์ฃ . ์ปค๋ง์ ์ด์ฉํ๋ฉด API ํธ์ถ ํจ์๋ฅผ ์ ๋ง ์ฐ์ํ๊ฒ ๋ง๋ค ์ ์์ด์!
function createAPI(baseURL) {
return function(endpoint) {
return function(method) {
return function(data) {
return fetch(`${baseURL}${endpoint}`, {
method: method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}).then(response => response.json());
}
}
}
}
const api = createAPI('https://api.example.com');
const usersAPI = api('/users');
const createUser = usersAPI('POST');
const updateUser = usersAPI('PUT');
// ์ฌ์ฉ ์์
createUser({name: 'John', age: 30}).then(user => console.log(user));
updateUser({id: 1, name: 'John Doe'}).then(user => console.log(user));
์ด๋ค๊ฐ์? ์ด๋ ๊ฒ ํ๋ฉด API ํธ์ถ ํจ์๋ฅผ ์ ๋ง ์ ์ฐํ๊ฒ ๋ง๋ค ์ ์์ด์. ์ฌ๋ฅ๋ท์์ ๋ฐฑ์๋ ๊ฐ๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ธฐ๋ฒ์ ๊ฐ๋ ฅํจ์ ๋ฐ๋ก ๋๋ผ์ค ์ ์์ ๊ฑฐ์์! ๐ช
2. ๐ญ ๋ฏธ๋ค์จ์ด ์ฒด์ธ ๋ง๋ค๊ธฐ
์๋ฒ ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด ๋ฏธ๋ค์จ์ด๋ฅผ ์์ฃผ ์ฌ์ฉํ๊ฒ ๋์ฃ . ์ปค๋ง์ ์ด์ฉํ๋ฉด ๋ฏธ๋ค์จ์ด ์ฒด์ธ์ ์์ฃผ ์ฐ์ํ๊ฒ ๋ง๋ค ์ ์์ด์.
function compose(...middlewares) {
return function(ctx, next) {
function dispatch(i) {
let fn = middlewares[i];
if (i === middlewares.length) fn = next;
if (!fn) return Promise.resolve();
try {
return Promise.resolve(fn(ctx, function next() {
return dispatch(i + 1);
}));
} catch (err) {
return Promise.reject(err);
}
}
return dispatch(0);
}
}
const logger = (ctx, next) => {
console.log(`${ctx.method} ${ctx.url}`);
return next();
};
const errorHandler = (ctx, next) => {
return next().catch(err => {
console.error(err);
ctx.status = err.status || 500;
ctx.body = {error: err.message};
});
};
const app = compose(errorHandler, logger);
// ์ฌ์ฉ ์์
app({method: 'GET', url: '/users'}, () => {
throw new Error('Something went wrong');
}).then(() => console.log('Done'));
์์ฐ! ์ด๋ ๊ฒ ํ๋ฉด ๋ฏธ๋ค์จ์ด๋ฅผ ์ ๋ง ์ ์ฐํ๊ฒ ์กฐํฉํ ์ ์์ด์. ์๋ฒ ๊ฐ๋ฐ์ด ํ์ธต ๋ ์ฌ๋ฏธ์์ด์ง ๊ฑฐ์์! ๐
3. ๐จ ์คํ์ผ ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ
ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ํ ๋ ์คํ์ผ ์ปดํฌ๋ํธ๋ฅผ ๋ง์ด ์ฌ์ฉํ์ฃ ? ์ปค๋ง์ ์ด์ฉํ๋ฉด ์ ๋ง ๋ฉ์ง ์คํ์ผ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์์ด์!
function styled(element) {
return function(strings, ...values) {
return function(props) {
const style = strings.reduce((acc, str, i) => {
return acc + str + (values[i] ? values[i](props) : '');
}, '');
const el = document.createElement(element);
el.style.cssText = style;
return el;
}
}
}
const Button = styled('button')`
background-color: ${props => props.primary ? 'blue' : 'gray'};
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
`;
// ์ฌ์ฉ ์์
const primaryButton = Button({primary: true});
document.body.appendChild(primaryButton);
const secondaryButton = Button({primary: false});
document.body.appendChild(secondaryButton);
์ด๋ค๊ฐ์? ์ด๋ ๊ฒ ํ๋ฉด ์คํ์ผ ์ปดํฌ๋ํธ๋ฅผ ์ ๋ง ์ฐ์ํ๊ฒ ๋ง๋ค ์ ์์ด์. ์ฌ๋ฅ๋ท์์ ํ๋ก ํธ์๋ ๊ฐ๋ฐ ๊ฐ์๋ฅผ ๋ค์ด๋ณธ ๋ถ๋ค์ด๋ผ๋ฉด ์ด ๊ธฐ๋ฒ์ ๋งค๋ ฅ์ ํน ๋น ์ง์ค ๊ฑฐ์์! ๐จ
๐ก Tip: ์ปค๋ง์ ์ฌ์ฉํ ๋๋ ํจ์์ ์ธ์ ์์๊ฐ ์ค์ํด์. ๊ฐ์ฅ ์์ฃผ ๋ณ๊ฒฝ๋๋ ์ธ์๋ฅผ ๋ง์ง๋ง์ ๋๋ ๊ฒ์ด ์ข๋ต๋๋ค!
๐ง ์ปค๋ง์ ์ฌํ ๊ฐ๋
์, ์ด์ ์ปค๋ง์ ๊ธฐ๋ณธ๊ณผ ์์ฉ์ ๋ํด ์์๋ดค์ผ๋, ์กฐ๊ธ ๋ ๊น์ด ๋ค์ด๊ฐ๋ณผ๊น์? ๐ต๏ธโโ๏ธ
1. ๐ ์๋ ์ปค๋ง (Auto-currying)
์ง๊ธ๊น์ง๋ ์๋์ผ๋ก ์ปค๋ง์ ๋ง๋ค์ด์์ง๋ง, ํจ์๋ฅผ ์๋์ผ๋ก ์ปค๋งํ๋ ๋ฐฉ๋ฒ๋ ์์ด์!
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
return function(...args2) {
return curried.apply(this, args.concat(args2));
}
}
};
}
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // 6
console.log(curriedAdd(1, 2)(3)); // 6
console.log(curriedAdd(1, 2, 3)); // 6
์! ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ค ํจ์๋ ์๋์ผ๋ก ์ปค๋งํ ์ ์์ด์. ์ ๋ง ํธ๋ฆฌํ์ฃ ? ๐
2. ๐ ์ปค๋ง๊ณผ ํจ์ ํฉ์ฑ (Function Composition)
์ปค๋ง์ ํจ์ ํฉ์ฑ๊ณผ ๊ถํฉ์ด ์ ๋ง ์ข์์. ๋ ๊ฐ๋ ์ ์กฐํฉํ๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ์ฝ๋๋ฅผ ๋ง๋ค ์ ์์ฃ !
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);
const add = x => y => x + y;
const multiply = x => y => x * y;
const divide = x => y => y / x;
const calculate = compose(
add(5),
multiply(2),
divide(4)
);
console.log(calculate(20)); // ((20 / 4) * 2) + 5 = 15
์ด๋ ๊ฒ ํ๋ฉด ๋ณต์กํ ์ํ ์ฐ์ฐ๋ ์์ฃผ ์ฐ์ํ๊ฒ ํํํ ์ ์์ด์. ๋ง์น ์ํ ๊ณต์์ ์ฐ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ฃ ! ๐งฎ
3. ๐ญ ์ปค๋ง ํฉํ ๋ฆฌ (Currying Factory)
์ปค๋ง์ ์ด์ฉํด ํจ์ ํฉํ ๋ฆฌ๋ฅผ ๋ง๋ค ์๋ ์์ด์. ์ด๋ฅผ ํตํด ๋น์ทํ ํจํด์ ํจ์๋ฅผ ์ฝ๊ฒ ์์ฑํ ์ ์์ฃ .
- ์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์ฌ๋ฅ๋ท์ ์ต์ AI ๊ธฐ์ ๊ณผ ๋ฒ๋ฅ ์ ๊ธฐ๋ฐํ์ฌ ์์ฌ์ ์ง์ ์ฌ์ฐ๊ถ์ ์ ๊ทน์ ์ผ๋ก ๋ณดํธํ๋ฉฐ,
๋ฌด๋จ ์ฌ์ฉ ๋ฐ ์นจํด ํ์์ ๋ํด ๋ฒ์ ๋์์ ํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ ํฉ๋๋ค.
ยฉ 2025 ์ฌ๋ฅ๋ท | All rights reserved.
๋๊ธ 0๊ฐ