Fun and games with lambda

S

Steven D'Aprano

If you've ever wondered what lambda and reduce are good for, run this one-
liner and wonder no more...

(Be patient, it may take a few seconds to return.)

# Python 2 version:

print((lambda f:((lambda p:p[0]+'.'+p[1:])(str((lambda Q:2*Q[0]*Q[0]//Q
[3])((lambda F:(lambda S:f(lambda T,_:((T[0]+T[1])//2,S((T[0]*T[1])//
F),2*T[2],(T[3]-(T[2]*(((T[0]+T[1])//2)**2-(S((T[0]*T[1])//F))**2))//F)),
[0]*13,(F,(F*F)//S(2*F),2,F//2)))(lambda n:f(lambda x,_:(x-x//2+(n*F)//
(2*x)),[0]*15,n//2)))(10**(5010))))[:5000])))(reduce))

# Python 3 version:

print((lambda f:((lambda p:p[0]+'.'+p[1:])(str((lambda Q:2*Q[0]*Q[0]//Q
[3])((lambda F:(lambda S:f(lambda T,_:((T[0]+T[1])//2,S((T[0]*T[1])//
F),2*T[2],(T[3]-(T[2]*(((T[0]+T[1])//2)**2-(S((T[0]*T[1])//F))**2))//F)),
[0]*13,(F,(F*F)//S(2*F),2,F//2)))(lambda n:f(lambda x,_:(x-x//2+(n*F)//
(2*x)),[0]*15,n//2)))(10**(5010))))[:5000])))(__import__
('functools').reduce))


I can't take credit for this little beauty. It originally came from
Manuel Garcia, all I did was make it compatible with Python 3 and add
some additional, but trivial, obfuscation.

See the original post here:
http://web.archiveorange.com/archive/v/5H3d1yQN5N15HEgOWHMx



Encouraging-hatred-of-lambdas-for-fun-and-profit-ly y'rs,
 
M

Mark Dickinson

print((lambda f:((lambda p:p[0]+'.'+p[1:])(str((lambda Q:2*Q[0]*Q[0]//Q
[3])((lambda F:(lambda S:f(lambda T,_:((T[0]+T[1])//2,S((T[0]*T[1])//
F),2*T[2],(T[3]-(T[2]*(((T[0]+T[1])//2)**2-(S((T[0]*T[1])//F))**2))//F)),
[0]*13,(F,(F*F)//S(2*F),2,F//2)))(lambda n:f(lambda x,_:(x-x//2+(n*F)//
(2*x)),[0]*15,n//2)))(10**(5010))))[:5000])))(reduce))

Very nice, but a little unnatural. Can't you find room to stick an
extra factor of 2 in there somewhere?

(See also: http://bugs.python.org/issue12345 )
 
C

Chris Angelico

If you've ever wondered what lambda and reduce are good for, run this one-
liner and wonder no more...

(Be patient, it may take a few seconds to return.)

I have a decent CPU so it's not too bad. And the precision produced is
noteworthy. However, I have no idea how it does its work, so I'm just
in awe of the quality of the code.

And you can read that last remark either way.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top