Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/curry_uncurry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def sum_args(*args):
return sum(args)

def curry(func, arity):
if arity < 0:
raise ValueError("Арность должна быть неотрицательной")

def curried(*args):
if len(args) >= arity:
return func(*args[:arity])

def next_curried(*next_args):
return curried(*args, *next_args)
return next_curried

return curried
Comment on lines +8 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ваша реализация допускает такое

def f(a, b, c):
    a + b + c

f_c = curry(f, 3)
assert f_c(1, 2)(3) == 6
assert f_c(1)(2, 3) == 6
assert f_c(1, 2, 3) == 6

Что не хорошо, потому что не вполне соответствует определению каррирования


def uncurry(curried_func, arity):
if arity < 0:
raise ValueError("Арность должна быть неотрицательной")

def uncurried(*args):
result = curried_func
for arg in args:
result = result(arg)
return result

return uncurried

10 changes: 10 additions & 0 deletions test/curring_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.curry_uncurry import sum_args, curry, uncurry

def test_curry():
sum3_curry = curry(sum_args, 3)
assert sum3_curry(1)(2)(3) == 6

def test_uncurry():
sum4_curry = curry(sum_args, 4)
sum4_uncurry = uncurry(sum4_curry, 4)
assert sum4_uncurry(1, 2, 3, 4) == 10