πŸ“ŒLanguage/JavaScript

TIL) 컀링(currying)κ³Ό ν΄λ‘œμ €(closure)의 차이 μ΄ν•΄ν•˜κΈ°, μžλ°”μŠ€ν¬λ¦½νŠΈ, js

hellohailie 2022. 6. 1. 00:02

 

 

 

μ»€λ§ν•¨μˆ˜μ™€ ν΄λ‘œμ €μ˜ 차이에 λŒ€ν•΄ μ•Œμ•„λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

μ»€λ§ν•¨μˆ˜κ°€ ν΄λ‘œμ €μ˜ μ‚¬μš© 사둀 쀑 ν•˜λ‚˜λΌκ³  λ§ν•˜λŠ” μ—”μ§€λ‹ˆμ–΄κ°€ μžˆλŠ” 반면,

ν΄λ‘œμ €λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 핡심 κΈ°λŠ₯이고, μ»€λ§ν•¨μˆ˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ— λ‚΄μž₯ μ§€μ›λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ™„μ „νžˆ λ‹€λ₯Έ κ°œλ…μ΄λΌκ³  λ§ν•˜λŠ” μ—”μ§€λ‹ˆμ–΄κ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

 


1. closure : μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 핡심 κΈ°λŠ₯ 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€.

ν΄λ‘œμ €λŠ” μ™ΈλΆ€ ν•¨μˆ˜ λ²”μœ„μ™€ μ™ΈλΆ€ ν•¨μˆ˜ μ™ΈλΆ€μ—μ„œ μ‹€ν–‰λ˜λŠ” κ²½μš°μ—λ„ μœ„μ˜ λͺ¨λ“  λ²”μœ„μ— μ•‘μ„ΈμŠ€ν•  수 μžˆλŠ” λ‚΄λΆ€ ν•¨μˆ˜μž…λ‹ˆλ‹€.

  • ν΄λ‘œμ €λ₯Ό μ–Έμ œ μ“ΈκΉŒ?

주둜 정보λ₯Ό 은닉할 λ•Œ μ‚¬μš©ν•˜κ³ , μ „μ—­ λ³€μˆ˜μ˜ μ‚¬μš©μ„ μ–΅μ œν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•©λ‹ˆλ‹€.

function numbers(firstNum, secondNum) {
	let a = 'The result is';
	return add();
}
function add() {
	let b = firstNum + secondNum;
	return a + b;
}
console.log(numbers(2,5));

// 좜λ ₯κ°’: ReferenceError: firstNum is not defined

add()κ°€ numbers() 외뢀에 μ •μ˜λ˜μ–΄ 있기 λ•Œλ¬Έμ— numbers()의 λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ firstNum이 μ •μ˜λ˜μ§€ μ•Šμ•˜λ‹€λŠ” 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

 

μœ„μ™€ 같은 λ²”μœ„λ‘œ 인해 λ°œν–‰ν•˜λŠ” 문제λ₯Ό κ·Ήλ³΅ν•˜κΈ° μœ„ν•΄ λ‚˜μ˜¨ 것이 λ°”λ‘œ closer μž…λ‹ˆλ‹€.

CLOSURES λŠ” λ³€μˆ˜ 및 μ „μ—­ λ³€μˆ˜ 외에 λ‹€λ₯Έ ν•¨μˆ˜μ˜ λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ν•  수 μžˆλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

function numbers(firstNum, secondNum) {
	let a = 'The result is ';
function add() {
	let b = firstNum + secondNum;
	return a + b;
}
	return add();
}

console.log(numbers(2,5));

// 좜λ ₯κ°’: The result is 7

이 경우 add()λŠ” numbers() 내뢀에 μ •μ˜λ˜μ–΄ 있으며 λͺ¨λ“  λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ 좜λ ₯은 ‘The result is 7’ 이 λ©λ‹ˆλ‹€.

 

 


2. currying: μžλ°”μŠ€ν¬λ¦½νŠΈμ— λ‚΄μž₯ 지원이 λ˜μ§€ μ•ŠλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

 

ν΄λ‘œμ €λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 핡심 κΈ°λŠ₯μ΄μ§€λ§Œ, μ»€λ§ν•¨μˆ˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ 자체적으둜 λ‚΄μž₯ 지원이 λ˜μ§€ μ•ŠλŠ” ν•¨μˆ˜λΌκ³  ν•©λ‹ˆλ‹€. μ œκ°€ μ˜μ–΄λ₯Ό λ²ˆμ—­ν•œ 것이라 μ–΄μƒ‰ν•˜κ²Œ 느껴질 수 도 μžˆμŠ΅λ‹ˆλ‹€. (no builtin support

 

컀링 ν•¨μˆ˜λŠ” ν΄λ‘œμ €μ˜ μ‚¬μš© 사둀 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€. 

컀링 ν•¨μˆ˜λŠ” ν•œ λ²ˆμ— λͺ¨λ“  인수λ₯Ό μ·¨ν•˜λŠ” λŒ€μ‹  μ—¬λŸ¬ 인수λ₯Ό ν•œ λ²ˆμ— ν•˜λ‚˜μ”© μ·¨ν•˜λŠ” ν•¨μˆ˜λ₯Ό λΆ„ν•΄ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

컀링 ν•¨μˆ˜μ—μ„œ 각 쀑첩 ν•¨μˆ˜λŠ” ν΄λ‘œμ €μ—μ„œ μ™ΈλΆ€ ν•¨μˆ˜μ— μ „λ‹¬λœ 인수λ₯Ό μΆ”μ ν•©λ‹ˆλ‹€.

 

  • 컀링을 μ–Έμ œ μ“ΈκΉŒ?

μ–΄λ–€ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ λŒ€λΆ€λΆ„μ˜ 맀개 λ³€μˆ˜κ°€ 항상 λΉ„μŠ·ν•˜λ‹€λ©΄ 컀링을 μ“°λ©΄ νŽΈν•˜λ‹€.

예λ₯Ό λ“€μ–΄, μ•„λž˜ μ½”λ“œλ₯Ό 컀링 ν•¨μˆ˜λ‘œ λ³€ν™˜ν•œλ‹€λ©΄

function multiply(x, y, z){
  return x, y, z;
}

console.log(multiply(3, 5, 7));  // 3*5*7
console.log(multiply(3, 5, 8));  // 3*5*8
console.log(multiply(3, 2, 1));  // 3*2*1

πŸ‘‡ μ»€λ§ν•¨μˆ˜λ‘œ λ³€ν™˜ν•œ μ½”λ“œ

function multiply(x) {
  return function(y) {
    return function(z){
      return x*y*z;
    }
  }
}

let multiply3 = multiply(3);   // 3이 고정됨
let multiply3And5 = multiply3(5);  // 3κ³Ό 5κ°€ 고정됨
let multiply3And2 = multiply3(2);  // 3κ³Ό 2κ°€ 고정됨
console.log(multiply3And5(7));
console.log(multiply3And5(8));
console.log(multiply3And2(1));

πŸ‘‡ λ‹€λ₯Έ 컀링 ν•¨μˆ˜μ˜ 예제 μ½”λ“œ

var sayWhat = function(a){
    return function(b){
        return function(c){
            console.log(`say ${a} to ${b} using ${c}`);
        }
    }
}

sayWhat("hello")("friends")("currying function"); // say hello to friends using currying function

 

 

πŸ’‘ ν˜Ήμ‹œ 잘λͺ»λœ 정보가 μžˆλ‹€λ©΄ λŒ“κΈ€ λ‚¨κ²¨μ£Όμ„Έμš”. μ €μ˜ μ„±μž₯에 λ§Žμ€ 도움이 λ©λ‹ˆλ‹€.

 

 

μ°Έκ³ 

https://codingnconcepts.com/javascript/lexical-scope-closures-and-currying/

https://www.quora.com/Is-there-any-difference-between-currying-and-closure-in-JavaScript