abuse of lambda question
Ben Wolfson
rumjuggler at cryptarchy.org
Tue Jun 6 23:49:31 EDT 2000
I'm attempting to abuse Python's lambdas, and I've come across a
problem I can't figure out. Well, the problem is actually that I'm
attempting to abuse lambdas, but aside from that I don't know why the
abuse is failing. Here's what I've got:
def __everyother(): #factory to return lambdas.
return lambda l, evens, odds, app=lambda a,e: lambda
a=a,e=e:a.append(e): Compose(apply(map, (lambda e, evens=evens,
odds=odds, l=l,app=app: If(l.index(e)%2==0, app(evens,e), app(odds,e))
,l)), Return(evens, odds))
The functions it calls are:
def Return(*args):
return lambda args=args: args
def Compose(*args):
return lambda fs=args: For(len(fs), lambda c, fs=fs:
If(lambda c=c,fs=fs: c==len(fs)-1, Return(fs[-1]()), fs[c]))()
def For(count, fct, *args, **kwargs):
for i in range(count):
fct(i,*args,**kwargs)
I can do this:
>>> import lambdautils
>>> a = lambdautils.__everyother()
>>> evens, odds = [], []
>>> a(range(20),evens, odds)
<function <lambda> at 1510c00>
>>> lam = a(range(20),evens, odds)
>>> evens
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> odds
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> lam()
Traceback (innermost last):
File "<pyshell#14>", line 1, in ?
lam()
File "C:\PYTHON16\lambdautils.py", line 16, in <lambda>
return lambda fs=args:\
File "C:\PYTHON16\lambdautils.py", line 4, in For
fct(i,*args,**kwargs)
File "C:\PYTHON16\lambdautils.py", line 17, in <lambda>
For(len(fs), lambda c, fs=fs: \
File "C:\PYTHON16\lambdautils.py", line 44, in If
return f()
TypeError: call of non-function (type list)
Can't quite figure out where the error occurs.
--
Barnabas T. Rumjuggler
Policeman: So sir, did the suspect have four arms, each one holding a
sword, and was she wearing a girdle adorned with severed hands, her
whole body covered in the blood of past victims?
Mr Flynn: Well, it was overcast and everything, so it was hard to say
for certain, you know?
-- John Flynn on apihna
More information about the Python-list
mailing list