Velocity Reviews > lambda in list comprehension acting funny

# lambda in list comprehension acting funny

88888 Dihedral
Guest
Posts: n/a

 07-12-2012
On Thursday, July 12, 2012 12:34:33 AM UTC+8, Ian wrote:
> On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams &lt;(E-Mail Removed)&gt; wrote:
> &gt; I don't understand why you would expect 1, 2, 4.
>
> Because:
>
> funcs[0](2) == 2 ** 0 == 1
> funcs[1](2) == 2 ** 1 == 2
> funcs[2](2) == 2 ** 2 == 4
>
> &gt; Perhaps parentheses will help the order of evaluation:
> &gt;
> &gt; funcs = [(lambda x: x**i) for i in range( 5 )]
> &gt;
> &gt; This gives:
> &gt; 1
> &gt; 16
> &gt; 81
>
> No, that gives 16, 16, 16 just like the original. I don't understand
> why you would expect 1, 16, 81, unless you have misread the code.

I'll contribute my way of python programming:

def powerb(x, b): #
return x**b

One functor is enough!

88888 Dihedral
Guest
Posts: n/a

 07-12-2012
On Thursday, July 12, 2012 12:34:33 AM UTC+8, Ian wrote:
> On Wed, Jul 11, 2012 at 4:28 AM, Colin J. Williams &lt;(E-Mail Removed)&gt; wrote:
> &gt; I don't understand why you would expect 1, 2, 4.
>
> Because:
>
> funcs[0](2) == 2 ** 0 == 1
> funcs[1](2) == 2 ** 1 == 2
> funcs[2](2) == 2 ** 2 == 4
>
> &gt; Perhaps parentheses will help the order of evaluation:
> &gt;
> &gt; funcs = [(lambda x: x**i) for i in range( 5 )]
> &gt;
> &gt; This gives:
> &gt; 1
> &gt; 16
> &gt; 81
>
> No, that gives 16, 16, 16 just like the original. I don't understand
> why you would expect 1, 16, 81, unless you have misread the code.

I'll contribute my way of python programming:

def powerb(x, b): #
return x**b

One functor is enough!

Steven D'Aprano
Guest
Posts: n/a

 07-12-2012
On Wed, 11 Jul 2012 20:39:45 -0700, 88888 Dihedral wrote:

> I'll contribute my way of python programming:
>
> def powerb(x, b): #
> return x**b

Here's a shorter version:

py> pow
<built-in function pow>

> One functor is enough!

Nothing we have been discussing in this thread has been a functor, either
in the Haskell sense or the C++ sense.

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 07-12-2012
On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote:

> {non sequitur: I still recall my archaic C++ class with the OOAD
> assignment of designing said calculator -- we never had to implement
> one, just design the basic classes/methods/attributes [on 3x5 cards] for
> a four-banger. I managed to persuade the team I was on that an RPN
> calculator would be simpler to (potentially) implement... And THEN
> persuaded them to NOT use the equivalent of an ALU class, but to put the
> math work into each operation button...

"ALU class"?

Googling gives me no clue.

--
Steven

Steven D'Aprano
Guest
Posts: n/a

 07-12-2012
On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote:

> funcs = [ lambda x: x**i for i in range( 5 ) ]

Here's another solution:

from functools import partial
funcs = [partial(lambda i, x: x**i, i) for i in range(5)]

Notice that the arguments i and x are defined in the opposite order.
That's because partial only applies positional arguments from the left.
If there was a "right partial" that applies from the right, we could use

funcs = [rpartial(pow, i) for i in range(5)]

--
Steven

Terry Reedy
Guest
Posts: n/a

 07-12-2012
On 7/11/2012 11:53 PM, Steven D'Aprano wrote:
> On Wed, 11 Jul 2012 21:05:30 -0400, Dennis Lee Bieber wrote:
>
>> {non sequitur: I still recall my archaic C++ class with the OOAD
>> assignment of designing said calculator -- we never had to implement
>> one, just design the basic classes/methods/attributes [on 3x5 cards] for
>> a four-banger. I managed to persuade the team I was on that an RPN
>> calculator would be simpler to (potentially) implement... And THEN
>> persuaded them to NOT use the equivalent of an ALU class, but to put the
>> math work into each operation button...

>
> "ALU class"?
>
> Googling gives me no clue.

4th hit: Arithmetic Logic Unit (hardware) -- Wikipedia
ALU class -- software version or simulation thereof.

The hardware version of Dennis's distributed design would be
interesting. Spreadsheets are based in part on the same idea of
distributed logic and arithmetic.

--
Terry Jan Reedy

Dennis Lee Bieber
Guest
Posts: n/a

 07-12-2012
On 12 Jul 2012 03:53:03 GMT, Steven D'Aprano
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

> "ALU class"?
>
> Googling gives me no clue.

Arithmetic/Logic Unit

http://en.wikipedia.org/wiki/Arithmetic_logic_unit
http://en.wikipedia.org/wiki/74181
{diversion: http://en.wikipedia.org/wiki/VAX-11/780 -- incredible...
that used to be considered a "super-mini" when I worked on them; and now
would be shamed by most laptops except for the ability to support so
many users concurrently (let me know when a Windows laptop supports 32
VT-100 class connections <G>)}

--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

88888 Dihedral
Guest
Posts: n/a

 07-12-2012
On Thursday, July 12, 2012 11:51:04 AM UTC+8, Steven D'Aprano wrote:
> On Wed, 11 Jul 2012 20:39:45 -0700, 88888 Dihedral wrote:
>
> &gt; I'll contribute my way of python programming:
> &gt;
> &gt; def powerb(x, b): #
> &gt; return x**b
>
> Here's a shorter version:
>
> py&gt; pow
> &lt;built-in function pow&gt;
>
>
> &gt; One functor is enough!
>
> Nothing we have been discussing in this thread has been a functor, either
> in the Haskell sense or the C++ sense.
>
>
>
> --
> Steven

Well, I encountered this kind of problems before in OOP.

I have to make sure my functor to keep the state variable values
for different objects that call the same functor to behave correctly
in order to avoid passing extra parameters in various objects using the same functor.

John O'Hagan
Guest
Posts: n/a

 07-12-2012
On Wed, 11 Jul 2012 13:21:34 -0700 (PDT)

> Exactly. It's threads like these which remind me why I never use lambda. I
> would rather give a function an explicit name and adhere to the familiar
> Python syntax, despite the two extra lines of code. I don't even like the
> name "lambda". It doesn't tell you what it is (unless you're John McCarthy),
> a function that you won't re-use and so you don't really need to give it a
> persistent name.
>
> I haven't seen any lambdas in any Python library code, or in any of the
> third-party modules I use (numpy, matplotlib, Biopython). Do they exist?
> Because I have not been forced to do so, I haven't retained a space in the
> top drawer of my programming brain for lambda.

[...]

+1 about the name, -1 about the usefulness. For example it's a clear and
concise way to pass a key argument to min, max, sort and friends:

sort(seq, key=lambda x: x.a)

The alternative is to have trivial function definitions floating around the
namespace which readers of the code have to search for.

--
John

Steven D'Aprano
Guest
Posts: n/a

 07-12-2012
On Wed, 11 Jul 2012 22:04:51 -0700, 88888 Dihedral wrote:

> I have to make sure my functor to keep the state variable values for
> different objects that call the same functor to behave correctly in
> order to avoid passing extra parameters in various objects using the
> same functor.

Yo dawg, I heard you liked functors, so I put a functor in your functor
so you can train your markov chains with extra functor parameters to
functor objects that are functor factory decorator functors.

--
Steven