[Python-ideas] Where-statement (Proposal for function expressions)
Gerald Britton
gerald.britton at gmail.com
Thu Jul 16 03:51:03 CEST 2009
foo(x,y) = myfunc(bar) where:
myfunc = lambda x:f(x)
bar = lambda y:g (y)
where:
f = lambda x:getattr(x, 'func')
g = lambda y:str(y)
would translate to:
def foo(x,y):
return getattr(x, 'func')str(y)
Basically Haskell reads like math:
circle_area = pi * radius**2 where pi is 3.14159 and radius is the
radius of the circle
You unpack it as you go. You could do the same this way:
f = lambda x:getattr(x,'func')
g = lambda y:str(y)
bar = lambda y: g(y)
myfunc = lambda x: f(x)
foo(x,y) = lambda x,y: myfunc(x)(bar(y))
though this is just a toy example to give the general flavor.
On Wed, Jul 15, 2009 at 7:12 PM, Carl
Johnson<cmjohnson.mailinglist at gmail.com> wrote:
> Daniel Stutzbach wrote:
>
>> Unlikely, but this is:
>>
>> total =3D sum(f(item) for item in lst) where:
>> def f(item):
>> return item.size if item.size > 0 else 1
>
> Yes, that seems much more sensible than trying to repeat the where's
> inside of list comp or gen exp.
>
> Daniel Stutzbach wrote:
>
>> How about the following as additional syntactic sugar for the common one-=
> function case?
>>
>> x =3D blah(f) where def f(item):
>> body_of_f
>
> I like it.
>
> Gerald Britton wrote:
>
>> Why not just use the Haskell approach?
>>
>> foo(x,y) =3D myfunc(bar) where:
>> =A0 =A0 =A0 =A0 =A0 myfunc, bar =3D f(x), g(y) where:
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0f,g =3D func1, func2
>
> Because I've been looking at that for a couple minutes and still have
> no idea what it is supposed to mean. :-( List comprehensions and
> pattern matching are the only Haskell features that make any sense to
> me.
>
> Jan Kaliszewski wrote:
>
>> I don't like the direction of limiting content of the block to set of sin=
> gular simple statements. I suppose where-block should simply create a separ=
> ate nested scope.
>
> Yes. It strikes me that one of the problems with Ruby is that there
> are different scoping rules for blocks, lambda, methods, procs, and
> whatever else=85 It would be much easier for users if "where" blocks had
> the exact same scoping rules as functions. Daniel Stutzbach's
> suggested equivalence strikes me as exactly correct:
>
> [return|yield|x=3D] expression_list where:
> suite
>
> is roughly equivalent to:
>
> def where_expression():
> suite
> return expression_list
> [return|yield|x=3D] where_expression()
>
>
> -- Carl Johnson
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
--
Gerald Britton
More information about the Python-ideas
mailing list