Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/6/curry_uncurry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def curry(func, max_args):
if max_args <= 0:
Copy link

Choose a reason for hiding this comment

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

А почему арность не может быть 0?

raise ValueError('max_args must be a positive integer')
Comment on lines +2 to +3
Copy link

Choose a reason for hiding this comment

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

Не проверяется случай, когда пользователь указывает арность не соответствующую реальной.
Мы можем написать f_curry = curry(sum3, 100), ошибка появится только при вызове функции f_curry.

Так же не проверяется, что max_args это int.


def arg(*args):
if len(args) == max_args:
return func(*args)

def new_arg(*new_args):
return arg(*(args + new_args))

return new_arg

return arg


def uncurry(curried_func, max_args):
def uncurry_next(*args):
current_func = curried_func
for arg in args:
current_func = current_func(arg)

return current_func
Comment on lines +18 to +23
Copy link

Choose a reason for hiding this comment

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

Нет проверки количества переданных аргументов len(args) != max_args


return uncurry_next


def f4(x, y, z, t):
return x + y + z + t


f_curry = curry(f4, 4)
f_uncurry = uncurry(f_curry, 4)
print(f_curry(3)(4)(5)(6))
print(f_uncurry(3, 4, 5, 6))