On Tue, Jun 29, 2021 at 7:15 PM Steven D'Aprano <st...@pearwood.info> wrote:
>
> On Tue, Jun 29, 2021 at 05:17:36PM +1000, Chris Angelico wrote:
>
> > What if you loop over the values in the range(0, 10) but skip all of
> > the odd numbers? Is that two concepts too?
>
> Of course it is. Think about somebody who knows about looping, and knows
> what range() does, but has no concept of odd numbers. (Perhaps a very
> precocious child.) They would easily understand `for i in range(0, 10, 2)`
> but have no idea how to skip odd numbers.
>
> If that example is too implausible for you, how about skipping the
> antisigma numbers?
>
> https://oeis.org/A024816
>
> Going back to odds and evens, you even described it as two steps:
>
> 1. loop over the values in the range(0, 10)
>
> 2. but skip all of the odd numbers.
>
> That's two distict steps.

My point is that the *iteration* isn't any different - the *range* is
what's different. A filtered range makes very good sense here. Hence,
a disjoint range object (one capable of subtraction) would be an
excellent solution to the OP's problem.

So why should it be fundamentally different to apply conditions?
Surely that's just another form of subsetting?

> > Because we have an easy way to spell that: range(0, 10, 2).
>
> Sure. But why do we care so much about this trivial special case?
> Remember that the condition can be as general as we like. How about
> looping over numbers between 0 and a trillion whose bit count (number of
> 1s in binary) is a perfect number, except for those which are prime?

Exactly.

> More practically, if you have some arbitrarily complicated iterable, and
> you wish to skip some of those items according to some arbitrarily
> complicated condition known only at runtime:
>
>     for url in filter(
>              lambda url: url not in skiplist,
>              map(make_url, webspider.follow_all(depth=10000))
>              ):
>         if (url not in seen and is_image(url.filetype)
>                 and image:=Image.read(url) and
>                 image.detect_faces(**params).match_any(*targets)
>                 ):
>             process(url)
>
> do you still think that's conceptually a single operation?
>
> "Process the URLs I want, duh!"

Well, that gets down to the question of what counts as a "sentence".
You shouldn't cram everything onto a single line of code just because
you can, just as you shouldn't write an English sentence ten pages
long.

But I don't see a problem with filtered iteration. We currently have a
very clunky way of spelling it ("for url in (x for x in iterable if
cond):"), and Python is perfectly happy to do this. There are plenty
of times when it would make very good sense to consider the condition
to be part of the iterable.

ChrisA
_______________________________________________
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/T7XU5NUEJG2CMLNXYYICI3MCGSAO476T/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to