Choice Python Hack: Infix Operators #
Pencils down. Cool hack for Python right here. I guess this was up on LtU a few days ago. I saw it on The Farm, so.
Works like:
# simple multiplication
x=Infix(lambda x,y: x*y)
print 2 |x| 4
# => 8
# class checking
isa=Infix(lambda x,y: x.__class__==y.__class__)
print [1,2,3] |isa| []
print [1,2,3] <<isa>> []
# => True
# inclusion checking
is_in=Infix(lambda x,y: y.has_key(x))
print 1 |is_in| {1:'one'}
print 1 <<is_in>> {1:'one'}
# => True
Anyway, this hacker—Ferdinand Jamitzky—sure would fit in well over here. He’s tread so close to our territory. What with the Ruby-like syntatic sugar he supplied for Python last year.
5 *times(lambda: printf("Hello"))
[1,2,3,4] *each(lambda x: printf("Count:"+str(x)))
print [1,2,3,4,5] *length
['a','b','c','d','e'] *each(lambda char: char+'!')
I mean if he can make Python look like Ruby, maybe he can make Ruby look like dolphin sonar. Can you best these??



