From 0c767890f075172b52cee317f32ac239b0a23355 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 26 Oct 2025 02:03:28 +0300 Subject: [PATCH 1/4] Add currying and uncurrying functions --- src/functions_exceptions/currying/currying.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/functions_exceptions/currying/currying.py diff --git a/src/functions_exceptions/currying/currying.py b/src/functions_exceptions/currying/currying.py new file mode 100644 index 0000000..de10e31 --- /dev/null +++ b/src/functions_exceptions/currying/currying.py @@ -0,0 +1,40 @@ +def curry(func, n): + if n < 0: + raise Exception("Negative n-ary.") + try: + func(*range(n)) + except TypeError: + raise Exception("Not right n-ary.") + + if n in (0, 1): + return func + + def curried_func(x): + def new_func(*args): + return func(x, *args) + + return curry(new_func, n - 1) + + return curried_func + + +def uncurry(curried_func, n): + if n < 0: + raise Exception("Negative n-ary.") + + if n == 0: + return curried_func + + def uncurried_func(*args): + new_func = curried_func(args[0]) + for x in range(1, n): + new_func = new_func(args[x]) + + return new_func + + try: + uncurried_func(*range(n)) + except TypeError: + raise Exception("Not right n-ary.") + + return uncurried_func From a98d89f79e1e52c0ed79858dd253c496d365ccbf Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 26 Oct 2025 12:19:30 +0300 Subject: [PATCH 2/4] Add configuration for pytest --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5d71f15 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[tool.pytest.ini_options] +testpaths = "tests" +pythonpath = "src" From 41e3d993cee6e69624e5a9ebff5efa798b6fc285 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 26 Oct 2025 12:20:00 +0300 Subject: [PATCH 3/4] Add tests for currying and uncurrying functions --- tests/currying_test.py | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/currying_test.py diff --git a/tests/currying_test.py b/tests/currying_test.py new file mode 100644 index 0000000..82ee637 --- /dev/null +++ b/tests/currying_test.py @@ -0,0 +1,81 @@ +from functions_exceptions.currying.currying import curry, uncurry + + +def test_base(): + def func(x, y, z): + return x + y + z + + curry_func = curry(func, 3) + uncurry_func = uncurry(curry_func, 3) + assert func(10, 20, 30) == curry_func(10)(20)(30) == uncurry_func(10, 20, 30) + + +def test_unknown_argument_count(): + def func(*args): + return sum(args) + + curry_func = curry(func, 5) + uncurry_func = uncurry(curry_func, 5) + assert ( + func(1, 2, 3, 4, 5) == curry_func(1)(2)(3)(4)(5) == uncurry_func(1, 2, 3, 4, 5) + ) + + +def test_no_arguments(): + def func(): + return 1 + + curry_func = curry(func, 0) + uncurry_func = uncurry(curry_func, 0) + assert func() == curry_func() == uncurry_func() + + +def test_one_argument(): + def func(x): + return x + + curry_func = curry(func, 1) + uncurry_func = uncurry(curry_func, 1) + assert func(1) == curry_func(1) == uncurry_func(1) + + +def test_curry_negative_n_ary(): + def func(x, y, z): + return x + y + z + + try: + curry(func, -1) + except Exception as e: + assert str(e) == "Negative n-ary." + + +def test_uncurry_negative_n_ary(): + def func(x, y, z): + return x + y + z + + try: + curry_func = curry(func, 3) + uncurry(curry_func, -1) + except Exception as e: + assert str(e) == "Negative n-ary." + + +def test_curry_not_right_n_ary(): + def func(x, y, z): + return x + y + z + + try: + curry(func, 4) + except Exception as e: + assert str(e) == "Not right n-ary." + + +def test_uncurry_not_right_n_ary(): + def func(x, y, z): + return x + y + z + + try: + curry_func = curry(func, 3) + uncurry(curry_func, 4) + except Exception as e: + assert str(e) == "Not right n-ary." From 27e7e32e7daaa8b8f2f3394ccc37439fa86e9c43 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 26 Oct 2025 12:23:53 +0300 Subject: [PATCH 4/4] Add pytest run in CI --- .github/workflows/pytest.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/pytest.yml diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..5e9923c --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,30 @@ +name: Python tests + +on: + push: + branches: ["*"] + pull_request: + branches: ["*"] + +jobs: + test: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' # или нужная версия + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + # pip install -r requirements.txt # если есть зависимости + + - name: Run tests + run: | + pytest -v