[Python-Dev] For/while/if statements/comprehension/generator expressions unification
Josiah Carlson
jcarlson at uci.edu
Tue Sep 20 02:30:34 CEST 2005
Alexander,
The essence of what you have proposed has been proposed (multiple times) before,
and I seem to remember it was shot down.
The below functions offer the equivalent of list comprehensions with a
final post-processing step.
def cross(*args):
if len(args) == 1:
for i in args[0]:
yield i
elif len(args) == 2:
for i in args[0]:
for j in args[1]:
yield i,j
else:
for i in args[0]:
for j in cross(*args[1:]):
yield (i,) + j
def cross_with_predicate(*args, **kwargs):
if not 'pred' in kwargs:
for i in cross(*args):
yield i
pred = kwargs['pred']
if len(args) > 1:
for i in cross(*args):
if pred(*i):
yield i
else:
for i in cross(*args):
if pred(i):
yield i
Feel free to use that code and/or modify it to your heart's content (be
careful of attempting to simplify cross, .
- Josiah
Alexander Myodov <maa_public at sinn.ru> wrote:
> Hello,
>
> Well, we have "for", "while" and "if". We also have statements,
> list generator expressions and list comprehensions. In
> comprehensions we have the opportunity to use several for-s and in a
> same expression (for i in range (0, 640) for j in range (0, 640)),
> in loops we cannot; in comprehensions, we can use if-s to filter the
> items; in loops, we cannot, and we have to write an inner comparison instead.
>
> My opinion is that in so smart language as Python is, it would be
> great to have them generalized, so that the features available in
> list comprehensions should be available in statements, vice versa.
> All three statements (for/while/if) could be joined
> together into a single common one, where any of these could be
> combined and used multiple times (but for the cases when a
> "else"/"elif" etc causes are required, old-school one-statements
> should be left); and for such common expressions, they should be
> equal for both loop statements and list comprehensions/generator
> expressions.
> That is, the following loops should be possible:
> -------
> # This gives us some sugar to get rid of unnecessary indentations
> for x in range (0, 640) for y in range (0, 480):
> -------
> for x in range (0, 640) if should_handle_this_column(x) \
> for y in range (0, 480) if should_handle_this_row(y):
> -------
> for nX, nY in (
> f(x), f(y)
> for x in range (0, 640) if should_handle_this_column(x)
> for y in range (0, 480) if should_handle_this_row(y)
> ):
> -------
> for x in range (0, 640) for y in range (0, 480) while(!timeout()):
> -------
> for x in range (0, 640) while(!timeout_pos_in_row()) for y in range (0, 480)
> while(!timeout_rows()):
> -------
> # And the latest and the hugest one:
> for x in range (0, 640) if should_handle_this_column(x) while(!timeout_pos_in_row()) \
> for y in range (0, 480) if should_handle_this_row(y) while(!timeout_rows() :
> -------
> # And almost the same as generator expression:
> for x, y in (
> f(x), f(y)
> for x in range (0, 640) if should_handle_this_column(x) while(!timeout_pos_in_row())
> for y in range (0, 480) if should_handle_this_row(y) while(!timeout_rows()
> )
> -------
>
> Hope I didn't miss something important...
>
> --
> With best regards,
> Alexander mailto:maa_public at sinn.ru
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/jcarlson%40uci.edu
More information about the Python-Dev
mailing list