-
Notifications
You must be signed in to change notification settings - Fork 0
add functions for task on functional programming #9
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,109 @@ | ||||||
| def chech_args_availability(*args): | ||||||
| ''' | ||||||
| Accepts an arbitary number of arguments and check their availability: | ||||||
| their number should be more than 2 and the last argument should be a list. | ||||||
| If arguments does not satisfy these conditions, it raise an error. | ||||||
| :param args: positional arguments | ||||||
| ''' | ||||||
| if len(args) < 2: | ||||||
| raise AssertionError('You should pass at least 2 argumnets into function: function name and list object') | ||||||
| if not isinstance(args[-1], list): | ||||||
| raise AssertionError('Last argument should be a list object') | ||||||
|
|
||||||
|
|
||||||
| def sequential_map(*args): | ||||||
| ''' | ||||||
| Accepts an arbitary number of functions and list object as the last argument. | ||||||
| Then consistently applies these functions to list. | ||||||
| :param args: arbitary number of functions and list object as the last argument | ||||||
| :return: modified list | ||||||
| ''' | ||||||
| # checking arguments availability | ||||||
| chech_args_availability(*args) | ||||||
|
|
||||||
| funcs = args[:-1] | ||||||
| lst = args[-1] | ||||||
| lst = func_chain(*funcs)(lst) | ||||||
|
|
||||||
| # convert result to list object | ||||||
| if not isinstance(lst, list): | ||||||
| lst = list(lst) | ||||||
|
|
||||||
| # round float values if all of them have a zero after point | ||||||
| if all([numb.is_integer() for numb in lst]): | ||||||
| lst = list(map(int, lst)) | ||||||
|
|
||||||
| return lst | ||||||
|
|
||||||
|
|
||||||
| def consensus_filter(*args): | ||||||
| ''' | ||||||
| Accepts an arbitary number of bool functions and list object as the last argument. | ||||||
| Then consistently applies these functions to list and return those elements of the list | ||||||
| which gave True after applying each of the functions | ||||||
| :param args: arbitary number of bool functions and list object as the last argument | ||||||
| :return: a slice of the original list | ||||||
| ''' | ||||||
|
|
||||||
| # checking arguments availability | ||||||
| chech_args_availability(*args) | ||||||
|
|
||||||
| funcs = args[:-1] | ||||||
| lst = args[-1] | ||||||
| for func in funcs: | ||||||
| lst = list(filter(func, lst)) | ||||||
|
|
||||||
| return lst | ||||||
|
|
||||||
|
|
||||||
| def conditional_reduce(func_1, func_2, lst): | ||||||
|
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. А эта функция уже должна делать |
||||||
| ''' | ||||||
| Accepts two functions and list object. | ||||||
| First function filters elements of the list and returns slice of the original list. | ||||||
| Second function accepts these elements and does some manipulations with them returning one value. | ||||||
| :param func_1: bool function which accepts arbitary number of arguments | ||||||
| :param func_2: function which accepts slice of the original list and returns single value | ||||||
| :return: single value | ||||||
| ''' | ||||||
| filtered_args = list(filter(func_1, lst)) | ||||||
| return func_2(*filtered_args) | ||||||
|
|
||||||
|
|
||||||
| def func_chain(*funcs): | ||||||
| ''' | ||||||
| Accepts an arbitary number of bool functions and integrate them into one function | ||||||
| :param funcs: arbitary number of functions | ||||||
| :return: some value(s) - result of consistent application of passed functions | ||||||
| ''' | ||||||
| def wrapper_around_func_chain(args): | ||||||
| ''' | ||||||
| Wrapper around func_chain function. Makes consistent application of functions | ||||||
| passed into decorator function - func_chain. | ||||||
| :param args: positional arguments | ||||||
|
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. В твоём случае args это 1 аргумент
Suggested change
|
||||||
| :return: function which is a result of consistent application of passed functions | ||||||
| ''' | ||||||
| for func in funcs: | ||||||
| args = func(args) | ||||||
| return args | ||||||
| return wrapper_around_func_chain | ||||||
|
|
||||||
|
|
||||||
| def multiple_partial(*funcs, **kwargs): | ||||||
| ''' | ||||||
| Accepts an arbitary number of functions and keyword arguments for them. | ||||||
| It “freezes” some portion of a function’s keyword arguments resulting in a new object | ||||||
| with a simplified signature. | ||||||
| :param funcs: list of functions | ||||||
| :kwargs: keyword arguments | ||||||
| :return: list of functions | ||||||
| ''' | ||||||
| def partial(func, *args, **kwargs): | ||||||
| def newfunc(*fargs, **fkwargs): | ||||||
| newkwargs = kwargs.copy() | ||||||
| newkwargs.update(fkwargs) | ||||||
| return func(*fargs, **newkwargs) | ||||||
| return newfunc | ||||||
|
Comment on lines
+100
to
+105
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. Можно объявить её отдельно, чтобы избежать очень высокого уровня вложенности |
||||||
| new_funcs = [] | ||||||
| for func in funcs: | ||||||
| new_funcs.append(partial(func, **kwargs)) | ||||||
| return new_funcs | ||||||
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.
Функции нужно было применять к каждому элементу контейнера, а не к контейнеру целиком, она же называется
map