[Python-ideas] Where-statement (Proposal for function expressions)

Gerald Britton gerald.britton at gmail.com
Wed Jul 15 15:38:07 CEST 2009


Why not just use the Haskell approach?

foo(x,y) = myfunc(bar) where:
           myfunc, bar = f(x), g(y) where:
              f,g = func1, func2

assuming func1 and func2 were previously defined, otherwise another
"where" clause could follow. That way, introducing the "where" clause
effectively causes foo to be defined as a function.  Applying that to
the previous example:

l = [i for i in myiterator if fi] where:
           f = f(i)

if "myiterator" was previously defined, or even:

 l = [i for i in myiterator if f(i)] where:
           myiterator, f = (i for i in xrange(10)), bool

if "myiterator" was not previously defined.

On Wed, Jul 15, 2009 at 8:49 AM, Daniel
Stutzbach<daniel at stutzbachenterprises.com> wrote:
> On Wed, Jul 15, 2009 at 2:55 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>>
>> One such extension might be a "where" block. Applied
>> to the current problem:
>>
>>  foo(f) where:
>>    def f(x):
>>      ...
>
> I like this proposal much more than any of the previous proposals.  It has
> some of the flavor of Lisp's "let", but puts the important line first
> instead of at the end.  In a nutshell, it says "define these symbols for
> this one line only".  On the downside, for the common case of wanting to
> define a single function, the body of the function must be indented twice.
>
> I suggest the following grammar and meaning, which would be referred to by
> the assignment, yield, return, and expression statements:
>
> where_expression ::= expression_list "where" ":" suite | expression_list
>
>
> It evaluates as follows.
>
> [return|yield|x=] expression_list where:
>    suite
>
> is roughly equivalent to:
>
> def where_expression():
>     suite
>     return expression_list
> [return|yield|x=] expression_list()
>
>
> How about the following as additional syntactic sugar for the common
> one-function case?
>
> x = blah(f) where def f(item):
>    body_of_f
>
> --
> Daniel Stutzbach, Ph.D.
> President, Stutzbach Enterprises, LLC
>
> _______________________________________________
> 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