Proposal: [... for ... while cond(x)]

2006-08-06 Thread Eighty
I suggest a new extension of the list comprehension syntax:

[x for x in xs while cond(x)]

which would be equivalent to

list(itertools.takewhile(cond, xs))

+ Since Python favors list comprehensions over map, filter, and reduce,
this would be the preferred way to do this
+ "Takewhile operations" occur often, at least for me
+ I don't think it would break any existing syntax

An analogous syntax for dropwhile would be nice, but I can't think of
one.

This is not a PEP because it's a very simple idea and probably not just
anyone (read: me) can write and submit one. If there has been a PEP for
this, I've missed it; if not, it would be nice if someone wrote one.

Discuss.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal: [... for ... while cond(x)]

2006-08-06 Thread Eighty

Duncan Booth wrote:
> Eighty wrote:
>
> > I suggest a new extension of the list comprehension syntax:
> >
> > [x for x in xs while cond(x)]
> >
> > which would be equivalent to
> >
> > list(itertools.takewhile(cond, xs))
> >
>
> What would this syntax offer that:
>
>[x for x in takewhile(cond, xs)]
>
> doesn't currently offer? (Apart, that is, from saving you 3 characters of
> typing)

The same thing that [f(x) for x in xs] offers that map(f, xs) doesn't,
and the same thing that [x for x in xs if f(x)] offers that filter(f,
xs) doesn't. It's more "pythonic". You can use an expression for cond
instead of a lambda.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal: [... for ... while cond(x)]

2006-08-08 Thread Eighty

Eighty wrote:
> I suggest a new extension of the list comprehension syntax:
>
> [x for x in xs while cond(x)]
>
> which would be equivalent to
>
> list(itertools.takewhile(cond, xs))
>
> + Since Python favors list comprehensions over map, filter, and reduce,
> this would be the preferred way to do this
> + "Takewhile operations" occur often, at least for me
> + I don't think it would break any existing syntax
>
> An analogous syntax for dropwhile would be nice, but I can't think of
> one.
>
> This is not a PEP because it's a very simple idea and probably not just
> anyone (read: me) can write and submit one. If there has been a PEP for
> this, I've missed it; if not, it would be nice if someone wrote one.
>
> Discuss.

So does no one have a comment on this? The one objection I can come up
with is that this would change the set builder notation semantics too
much, but since the iteration order in a list comprehension is already
well defined, I don't think that would be the case. However, for this
reason, it wouldn't fit in a dict comprehension, if that would ever be
made, perhaps making the syntax inconsistent?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal: [... for ... while cond(x)]

2006-08-08 Thread Eighty

Terry Reedy wrote:
> whereas the analogous expansion of your proposal
>
> for x in xs:
>   while cond(x):
>  yield e(x)
>
> is an infinite loop and not at all what you mean.

You're right. The syntax is ambiguous. I agree it's not a good idea,
now. :)

> x for x in xs while cond(x) if blah(x)
> x for x in xs if blah(x) while cond(x)
> x*y for x in xs while cond(x) for y in ys

These wouldn't be a problem.
"... for x in xs while cond(x) ..." would be transformed into "... for
x in takewhile(cond, xs) ..."
which could be applied to an if thingy if you first transform
"... for x in xs if cond(x) ..." into "... for x in filter(cond, xs)
...".

-- 
http://mail.python.org/mailman/listinfo/python-list