[Baypiggies] lambda for newbies -- a true newbie question

Kelly Yancey kelly at nttmcl.com
Tue May 15 20:59:17 CEST 2007

Laurence Clark wrote::
> On 5/11/07, David Berthelot <d_berthelot at yahoo.com> wrote: "I often
> use it to pass basic functions (that are so basic that keeping them
> unnamed is perfect) to other functions. If I have to name all those
> unnamed lambda functions to use them, that's going to be quite
> verbose, probably a bit too much."
> Pardon the dumb question, but surely there must be some other way in
>  python to put little anonymous functions into expressions?? Some way
> to say:
> my $hundreda = myaddfunction ( sub{return 99;}, 1);
 > my $hundredb = sub{return 99;}->() + 1;
> Comments anyone?

   Perhaps I'm missing the point of your question, but your examples 
aren't that different when expressed in python syntax...

	myaddfunction = lambda *args: sum(args)

	hundreda = myaddfunction((lambda: 99)() , 1)
	hundredb = (lambda: 99)() + 1

   In fact they are slightly more concise than the perl equivalents. 
The key is to use parenthesis to encapsulate the lambda expression (sort 
of like you need brackets in perl to do the same).  Then you can call 
the lambda expression just like any other function.


Kelly Yancey

More information about the Baypiggies mailing list