Securing a future for anonymous functions in Python

Doug Holton a at b.c
Fri Dec 31 18:09:53 EST 2004


Steven Bethard wrote:

> Simo Melenius wrote:
> 
>> map (def x:
>>          if foo (x):
>>              return baz_1 (x)
>>          elif bar (x):
>>              return baz_2 (x)
>>          else:
>>              global hab
>>              hab.append (x)
>>              return baz_3 (hab),
>>      [1,2,3,4,5,6])
> 
> 
> I think this would probably have to be written as:

Right the comma plus other things make this difficult for a parser to 
handle correctly.  Other people have already come up with working solutions.

We have a special way to pass a multiline closure as a parameter to a 
function.  Put it outside the parameter list.

First, the single-line way using curly braces:

newlist = map({x as int | return x*x*x}, [1,2,3,4,5,6])

Then the multi-line way.  I had to add an overload of map to support 
reversing the order of parameters (list first, then the closure):

newlist = map([1,2,3,4,5,6]) def (x as int):
      return x*x*x

for item in newlist:
      print item




More information about the Python-list mailing list