>
> Earlier on the thread, I made a similar point that it would be nice to
> have a way to filter without the redundant for x in x. Though I can’t think
> of a really good way to express it. But as for filtered for loops:
>
> "for thing in
> (x for x in collection if is_interesting(x))"
>

It's pretty important that comprehensions can express transformations (i.e.
map) along with filters.  A lot of the examples and argument for extension
of `filter()` to general loops miss this point.

Yes, `x for x in stuff if pred(x)` looks kinda redundant.

But `transform(x) for x in stuff if pred(x)` really doesn't.  And
transformation in my mind is at least as common and at least as
important as filtering.

Happily, I can write this in Python right now:

items = (transform(x) for x in stuff if pred(x))

for item in items:

    foo = something_with(item)

    bar = something_else(item)

    if result(foo, bar): ...

I'm not making a slippery slope argument, I do know one thing is possible
without the other.  But if you really want an "entire comprehensions in a
loop statement" we wind up with something like:

for item := transform(x) for x in stuff if pred(x):

    foo = something_with(item)
    ...


Which is obviously a *possible* language, but I'd be -100 on making loop
statements that complicated needlessly.  The way we have now is great, and
clear, and already exists.  Other spellings are available as well, of
course, such as `if not transform(x): continue`.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/G7MK37M2CDL6MVAOBCEVH7A427MA2LWP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to