Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7830 > unrolled thread

Fun and games with lambda

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-06-17 16:10 +0000
Last post2011-06-18 10:40 +1000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Fun and games with lambda Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-17 16:10 +0000
    Re: Fun and games with lambda Wolfgang Rohdewald <wolfgang@rohdewald.de> - 2011-06-17 18:56 +0200
    Re: Fun and games with lambda Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-17 11:07 -0600
    Re: Fun and games with lambda Mark Dickinson <mdickinson@enthought.com> - 2011-06-17 12:02 -0700
    Re: Fun and games with lambda Chris Angelico <rosuav@gmail.com> - 2011-06-18 10:40 +1000

#7830 — Fun and games with lambda

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-06-17 16:10 +0000
SubjectFun and games with lambda
Message-ID<4dfb7c6d$0$30002$c3e8da3$5496439d@news.astraweb.com>
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,


-- 
Steven

[toc] | [next] | [standalone]


#7833

FromWolfgang Rohdewald <wolfgang@rohdewald.de>
Date2011-06-17 18:56 +0200
Message-ID<mailman.79.1308329807.1164.python-list@python.org>
In reply to#7830
On Freitag 17 Juni 2011, Steven D'Aprano wrote:
> run this one-
> liner and wonder no more...

looks like something dangerous to me. What does
it do? rm -rf ?

-- 
Wolfgang

[toc] | [prev] | [next] | [standalone]


#7837

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-17 11:07 -0600
Message-ID<mailman.83.1308330495.1164.python-list@python.org>
In reply to#7830
On Fri, Jun 17, 2011 at 10:56 AM, Wolfgang Rohdewald
<wolfgang@rohdewald.de> wrote:
> On Freitag 17 Juni 2011, Steven D'Aprano wrote:
>> run this one-
>> liner and wonder no more...
>
> looks like something dangerous to me. What does
> it do? rm -rf ?

The thread at the link discusses what it does in great detail.

[toc] | [prev] | [next] | [standalone]


#7845

FromMark Dickinson <mdickinson@enthought.com>
Date2011-06-17 12:02 -0700
Message-ID<db6f32b3-bb74-4704-ad8a-52e2d91665ff@j31g2000yqe.googlegroups.com>
In reply to#7830
On Jun 17, 5:10 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
>
> 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 )

--
Mark

[toc] | [prev] | [next] | [standalone]


#7872

FromChris Angelico <rosuav@gmail.com>
Date2011-06-18 10:40 +1000
Message-ID<mailman.99.1308357640.1164.python-list@python.org>
In reply to#7830
On Sat, Jun 18, 2011 at 2:10 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web