-
Notifications
You must be signed in to change notification settings - Fork 0
Curry uncurry task #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| def curry(func, max_args): | ||
| if max_args <= 0: | ||
| raise ValueError('max_args must be a positive integer') | ||
|
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не проверяется случай, когда пользователь указывает арность не соответствующую реальной. Так же не проверяется, что |
||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нет проверки количества переданных аргументов |
||
|
|
||
| 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)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А почему арность не может быть 0?