Skip to content
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

@composable decorator #27

Open
kachayev opened this issue Mar 27, 2013 · 3 comments
Open

@composable decorator #27

kachayev opened this issue Mar 27, 2013 · 3 comments
Assignees
Labels

Comments

@kachayev
Copy link
Owner

Special @composable decorator to bring new functionality to function:

  • easy way to compose functions (using one of standard operators)
  • overload apply operator
@ghost ghost assigned kachayev Mar 27, 2013
@berrytj
Copy link

berrytj commented Aug 17, 2014

This seems interesting, are you still thinking about it?

@kachayev
Copy link
Owner Author

@berrytj Yeah, I still think about it. I just don't want to make obvious mistake: to write decorator which will require special decorators order in each case. Which means that I want @composable decorator to be composable itself.

@berrytj
Copy link

berrytj commented Aug 20, 2014

(Stating the obvious for a moment.) composable must return a class for operator overrides to work. So any decorator that returns only functions will fail if called after composable, e.g.:

@memoize
@composable
def double(n):
    return n*2

(double * sum)([1, 2])  # => TypeError

I can only think of two ways around this, neither ideal. The first is to modify your decorators inline:

def classdecorator(decorator):
  def inner(f):
    if isinstance(f, types.FunctionType):
      return decorator(f)
    else:
      f.__call__ = decorator(f.__call__)
      return f
  return inner


@classdecorator(memoize)
@composable
def double(n):
  return n*2

(double * sum)([1, 2])  # => works

The second is to use libraries whose decorators have already undergone this operation (to support classes masquerading as functions). The toolz folks might allow that functionality addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants