[Python-ideas] Infix application of binary functions (original) (raw)
Carl M. Johnson cmjohnson.mailinglist at gmail.com
Thu Jul 22 05:49:16 CEST 2010
- Previous message: [Python-ideas] Infix application of binary functions
- Next message: [Python-ideas] Infix application of binary functions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Thought about it some more. Here’s a more general formula:
class InfixArity(object): def init(self, arity): self.arity = arity self.args = []
def __call__(self, func):
self.func = func
return self
def __add__(self, arg):
self.args.append(arg)
if len(self.args) < self.arity:
return self
else:
return self.func(*self.args)
__radd__ = __add__
Infix = lambda func: InfixArity(2)(func)
And of course, one can use mul or div or whatever to taste. "1 // add // 2” doesn’t make me instantly vomit in my mouth. ;-)
-- Carl Johnson
- Previous message: [Python-ideas] Infix application of binary functions
- Next message: [Python-ideas] Infix application of binary functions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]