From 36147ecaf318bbd9f7abc8901064d3ae969c2914 Mon Sep 17 00:00:00 2001 From: JaeBeen95 Date: Mon, 10 Mar 2025 17:21:29 +0900 Subject: [PATCH 1/2] =?UTF-8?q?curry=20=ED=95=A8=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/curry.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/curry.js diff --git a/src/curry.js b/src/curry.js new file mode 100644 index 0000000..e3c9bc7 --- /dev/null +++ b/src/curry.js @@ -0,0 +1,11 @@ +function curry(fn) { + return function curried(...args) { + if (args.length >= fn.length) { + return fn(...args); + } else { + return function recursion(...newArgs) { + return curried(...args, ...newArgs); + }; + } + }; +} From 59cece05924b6c15ca93d0e56e496880284135e9 Mon Sep 17 00:00:00 2001 From: JaeBeen95 Date: Mon, 10 Mar 2025 17:28:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?curry=20=ED=95=A8=EC=88=98=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/curry.js | 2 +- src/test/curry.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/test/curry.test.js diff --git a/src/curry.js b/src/curry.js index e3c9bc7..eea2f5f 100644 --- a/src/curry.js +++ b/src/curry.js @@ -1,4 +1,4 @@ -function curry(fn) { +export default function curry(fn) { return function curried(...args) { if (args.length >= fn.length) { return fn(...args); diff --git a/src/test/curry.test.js b/src/test/curry.test.js new file mode 100644 index 0000000..5632e4c --- /dev/null +++ b/src/test/curry.test.js @@ -0,0 +1,38 @@ +import curry from "../curry"; + +describe("curry 테스트", () => { + const sum = (a, b, c) => a + b + c; + const join = (a, b, c) => `${a}_${b}_${c}`; + + test("모든 인자를 한 번에 넘겨 호출 했을 때", () => { + const curriedSum = curry(sum); + const curriedJoin = curry(join); + + expect(curriedSum(1, 2, 3)).toBe(6); + expect(curriedJoin(1, 2, 3)).toBe("1_2_3"); + }); + + test("인자를 하나씩 전달했을 때", () => { + const curriedSum = curry(sum); + const curriedJoin = curry(join); + + expect(curriedSum(1)(2)(3)).toBe(6); + expect(curriedJoin(1)(2)(3)).toBe("1_2_3"); + }); + + test("여러 그룹으로 인자를 나누어 호출했을 때", () => { + const curriedSum = curry(sum); + const curriedJoin = curry(join); + + expect(curriedSum(1, 2)(3)).toBe(6); + expect(curriedSum(1)(2, 3)).toBe(6); + expect(curriedJoin(1, 2)(3)).toBe("1_2_3"); + expect(curriedJoin(1)(2, 3)).toBe("1_2_3"); + }); + + test("인자가 추가적으로 있을 때", () => { + const curriedSum = curry(sum); + + expect(curriedSum(1, 2, 3, 4)).toBe(6); + }); +});