Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.pytest.ini_options]
testpaths = "tests"
pythonpath = "src"
40 changes: 40 additions & 0 deletions src/functions_exceptions/currying/currying.py
Original file line number Diff line number Diff line change
@@ -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
81 changes: 81 additions & 0 deletions tests/currying_test.py
Original file line number Diff line number Diff line change
@@ -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."