커링(Curring Function)

102sae·2024년 2월 21일

구름톤 트레이닝

목록 보기
3/3

f(a, b, c)처럼 단일 호출로 처리하는 함수를
f(a)(b)(c)와 같이 각각의 인수가 호출 가능한 프로세스로 호출된 후 병합될 수 있게 변환하는 것.

커링은 함수를 호출하는 것이 아닌 변환하는 것이다!!

 	  const sum = (a, b) => a + b;
	  console.log(sum(1, 2));
      const curryingSum = (a) => (b) => a + b;
      console.log(curryingSum(1)(2));
      console.log(curryingSum(1));


매개변수를 부족하게 호출하면 필요한 매개변수를 console에서 알려줌.

 const makeFood = (ingredient1) => {
        return (ingredient2) => {
          return (ingredient3) => {
            return `${ingredient1} ${ingredient2} ${ingredient3}`;
          };
        };
      };

      const hambuger = makeFood("bread")("ham")("ketchup");
      console.log(hambuger);

      const cleanerMakeFood = (ingredient1) => (ingredient2) => (ingredient3) =>
        `${ingredient1} ${ingredient2} ${ingredient3}`;

      const newHambuger = cleanerMakeFood("bread")("ham")("ketchup");

다이나믹하게 커링 함수 호출하는법

파라미터가 몇개가 되든 맞춰서 해줌

참고자료
https://www.inflearn.com/course/%EB%94%B0%EB%9D%BC%ED%95%98%EB%A9%B0-%EB%B0%B0%EC%9A%B0%EB%8A%94-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8/dashboard

0개의 댓글