class infix(object):
"""
Clever hack (slightly modified) for defining infix operators.
Via http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122
Intended for use as a decorator.
>>> @infix
... def removing(str, chars):
... for char in chars:
... str = str.replace(char, '')
... return str
...
>>> print 'Hello, world!' |removing| 'eo'
Hll, wrld!
"""
def __init__(self, function):
self.function = function
def __ror__(self, other):
return infix(lambda x: self.function(other, x))
def __or__(self, other):
return self.function(other)
def __call__(self, value1, value2):
return self.function(value1, value2)
@infix
def λ(str1, str2):
return eval('lambda '+str1+':'+str2)
print(('x,y' |λ| 'x + y')(
Python实现中缀lambda λ算子和Y-组合子
最新推荐文章于 2022-10-16 10:23:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)