-
Description create a function (0) that takes a function_ (1) and returns a decorator (2) that calls function_ (1) on the output of the func (3) the decorator (2) reads.
Okay, that was confusing. We want to be able to apply a func to the output of another func, in a way that affects the func (e.g. it might be easiest to write a func using yield, but that causes issues if you want to memoize it and reuse the output)
Code def add_func(operator): def inner(func): def thingy(*args, **kwargs): return operator( func(*args, **kwargs)) return thingy return inner
Test Cases import functools Test.assert_equals(add_func(tuple)(range)(5), tuple(range(5)) ) .lru_cache(None) (list) def vals(n): for i in range(n): yield n+i Test.assert_equals(tuple(vals(5)), tuple(vals(5)) ) Test.assert_equals(vals(5), list(range(5,10)) )
Output:
-
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
Please sign in or sign up to leave a comment.