From d96c9b7e1b5224911d7711cb402b1d440eb45ec3 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 03:39:30 +0300 Subject: [PATCH 1/6] Implemented curry and uncurry functions --- src/Curry/curry.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Curry/curry.py diff --git a/src/Curry/curry.py b/src/Curry/curry.py new file mode 100644 index 0000000..5c37b0d --- /dev/null +++ b/src/Curry/curry.py @@ -0,0 +1,50 @@ +def curry(func, arity): + if not callable(func): + raise TypeError("Expected callable function") + + if not isinstance(arity, int) or arity <= 0: + raise ValueError("Arity must be a positive integer") + + def curried(*args, **kwargs): + total_args = len(args) + len(kwargs) + if total_args > arity: + raise TypeError("Too many arguments") + + if total_args >= arity: + return func(*args, **kwargs) + + def partial(*more_args, **more_kwargs): + new_args = args + more_args + new_kwargs = {**kwargs, **more_kwargs} + return curried(*new_args, **new_kwargs) + + return partial + + return curried + +def uncurry(curried_func, arity): + if not callable(curried_func): + raise TypeError("Expected callable function") + + if not isinstance(arity, int) or arity <= 0: + raise ValueError("Arity must be a positive integer") + + def uncurried(*args): + if len(args) != arity: + raise TypeError(f"Expected {arity} arguments, got {len(args)}") + + result = curried_func + for arg in args: + result = result(arg) + return result + + return uncurried + + +def sum3(x, y, z): + return x + y + z + +sum3_curry = curry(sum3, 3) +sum3_uncurry = uncurry(sum3_curry, 3) +print(sum3_curry(1)(2)(3)) # 6 +print(sum3_uncurry(1, 2, 3)) # 6 From fb057b4438d8cff502c58cb8c650e385a2fa707c Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 03:43:38 +0300 Subject: [PATCH 2/6] Formatted code --- src/Curry/curry.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Curry/curry.py b/src/Curry/curry.py index 5c37b0d..6549ddc 100644 --- a/src/Curry/curry.py +++ b/src/Curry/curry.py @@ -1,50 +1,52 @@ def curry(func, arity): if not callable(func): raise TypeError("Expected callable function") - + if not isinstance(arity, int) or arity <= 0: raise ValueError("Arity must be a positive integer") - + def curried(*args, **kwargs): total_args = len(args) + len(kwargs) if total_args > arity: raise TypeError("Too many arguments") - + if total_args >= arity: return func(*args, **kwargs) - + def partial(*more_args, **more_kwargs): new_args = args + more_args new_kwargs = {**kwargs, **more_kwargs} return curried(*new_args, **new_kwargs) - + return partial - + return curried + def uncurry(curried_func, arity): if not callable(curried_func): raise TypeError("Expected callable function") - + if not isinstance(arity, int) or arity <= 0: raise ValueError("Arity must be a positive integer") - + def uncurried(*args): if len(args) != arity: raise TypeError(f"Expected {arity} arguments, got {len(args)}") - + result = curried_func for arg in args: result = result(arg) return result - + return uncurried def sum3(x, y, z): return x + y + z + sum3_curry = curry(sum3, 3) sum3_uncurry = uncurry(sum3_curry, 3) -print(sum3_curry(1)(2)(3)) # 6 -print(sum3_uncurry(1, 2, 3)) # 6 +print(sum3_curry(1)(2)(3)) # 6 +print(sum3_uncurry(1, 2, 3)) # 6 From d70a089fe0b58a630aec1b8797d4c1d3c46f952f Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 09:13:18 +0300 Subject: [PATCH 3/6] Added requirements.txt --- requirements.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 From b1e794b7704a9d78355bc9a61fdd0c2d4bebd3e9 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 09:35:36 +0300 Subject: [PATCH 4/6] Added requirements and a stub test --- requirements.txt | 2 ++ src/Curry/test_stub.py | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 src/Curry/test_stub.py diff --git a/requirements.txt b/requirements.txt index e69de29..841f2a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,2 @@ +pytest +ruff \ No newline at end of file diff --git a/src/Curry/test_stub.py b/src/Curry/test_stub.py new file mode 100644 index 0000000..2953351 --- /dev/null +++ b/src/Curry/test_stub.py @@ -0,0 +1,5 @@ +import pytest + +def test_curry_stub(): + assert True + \ No newline at end of file From 82c55a31d5e42142bf5695ca8fa2aef879747c34 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 09:37:44 +0300 Subject: [PATCH 5/6] Formatted files --- src/Curry/test_stub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Curry/test_stub.py b/src/Curry/test_stub.py index 2953351..41b7e2e 100644 --- a/src/Curry/test_stub.py +++ b/src/Curry/test_stub.py @@ -1,5 +1,5 @@ import pytest + def test_curry_stub(): assert True - \ No newline at end of file From ef5058f58e0bf449f0cc3619d2430a1315f50c23 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Mon, 17 Nov 2025 09:40:35 +0300 Subject: [PATCH 6/6] Reformatted test_stub.py --- src/Curry/test_stub.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Curry/test_stub.py b/src/Curry/test_stub.py index 41b7e2e..2868ef2 100644 --- a/src/Curry/test_stub.py +++ b/src/Curry/test_stub.py @@ -1,5 +1,2 @@ -import pytest - - def test_curry_stub(): assert True