Fun with lambda and map

Drew Csillag drew_csillag at yahoo.com
Tue Jan 29 10:08:49 EST 2002


On Tue, Jan 29, 2002 at 09:44:31AM -0200, Gustavo Niemeyer wrote:
> > primes = lambda o:map(lambda a:filter(None,(map(lambda i:map(lambda
> > x:a.__setitem__(x,0),range(2*i,o,i)),range(2,o)),a)[1])[1:],[range(o)])[0]
> > 
> > print primes(20)
> > 
> > fibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda
> > l:o.__setslice__(l[0],l[1],l[2]),([o[2]+3,o[2]+4,[o[0]]],[0,3,[o[1],reduce(lambda
> > x,o:x+o,o[:2]),o[2]+1]])),range(x)),o)[1],[[1,1,0]+range(x)])[0][3:]
> > 
> > print fibonacci(20)
> 
> That proves you can write bad code in any language, if you really
> want to...

Oh, but with Python at least a) it's not the default, and b) it can
actually be a fun, creative experience to write one-liners (reminds me
of writing LISP code for some reason).

#this one really only works if your terminal window is 80 wide
import os;map(lambda i:os.write(1,chr(reduce(lambda(z,t),o:abs(z*z)>4and(6,
t)or(z*z+o[0],t+1),[(i%80*.04875-2.6+i/80*2.8j/26-1.4j,65)]*31)[1])),range
(2080))

#the screen of e
print'e=%s'%(lambda x:'%s.%s'%(x[0],x[1:-1]))(str(reduce(lambda
(a,b),y,z=10L**1835:(a+z/b,b*y),[[0L,1L]]+range(1,752))[0]))

#oh, when is easter?
print(lambda(m,d):'%s %s'%({3:'March',4:'April'}[m],d))((lambda(m,d):(m
==3and d>31)and(4,d-31)or(m,d))((lambda(m,d,y):(m,d+7-(lambda(m,d,y):(23
*m/9+d+4+y/4-y/100+y/400)%7)(m<3and(m,d+y,y-1)or(m,d+(y-2),y))))((lambda
(p,y):(p>31)and(4,p-31,y)or(3,p,y))((lambda y:(50-((11*((y%19)+1)-y/100+
(y/100)/4+(8*(y/100+11)/25))%30),y))(int(raw_input('year>')))))))

Drew




More information about the Python-list mailing list