[David Mertz]
> Do you anticipate that the `len()` function will be able to solve the
> Halting Problem?
>
It is simply not possible to know whether a given iterator will produce
> finitely many or infinitely many elements. Even those that will produce
> finitely many do not, in general, have a knowable length without running
> them until exhaustion.
You don't have to solve the halting problem. You simply ask the object. The
default behavior would be "I don't know" whether that's communicated by
returning None or some other sentinel value (NaN?) or by raising a special
exception. Then you simply override the default behavior for cases where
the object does or at least might know. itertools.repeat, for example;
would have an infinite length unless "times" is provided, then its length
would be the value of "times". map would return the length of the shortest
iterable unless there is an unknown sized iterable, then len would be
unknown, if all iterables are infinite, the length would be infinite.
We could add a decorator for length and/or length hints on generator
functions:
@length(lambda times: times or float("+inf"))*def* repeat(obj, times=None):
if times is None:
while True:
yield obj
else:
for i in range(times):
yield obj
On Thu, Nov 29, 2018 at 10:40 AM David Mertz <[email protected]> wrote:
> On Thu, Nov 29, 2018 at 2:29 AM Adrien Ricocotam <[email protected]>
> wrote:
>
>> Some suggested above to change the definition of len in the long term.
>> Then I think it could be interesting to define len such as :
>>
>> - If has a finite length : return that length (the way it works now)
>> - If has a length that is infinity : return infinity
>> - If has no length : return None
>>
>
> Do you anticipate that the `len()` function will be able to solve the
> Halting Problem?
>
> It is simply not possible to know whether a given iterator will produce
> finitely many or infinitely many elements. Even those that will produce
> finitely many do not, in general, have a knowable length without running
> them until exhaustion.
>
> Here's a trivial example:
>
> >>> def seq():
> ... while random() > 0.1:
> ... yield 1
> >>> len(seq())
> # What answer do you want here?
>
> Here's a slightly less trivial one:
>
> In [1]: from itertools import count
> In [2]: def mandelbrot(z):
> ...: "Yield each value until escape iteration"
> ...: c = z
> ...: for n in count():
> ...: if abs(z) > 2:
> ...: return n
> ...: yield z
> ...: z = z*z + c
>
> What should len(mandelbrot(my_complex_number)) be? Hint, depending on the
> complex number chosen, it might be any Natural Number (or it might not
> terminate).
>
>
> --
> 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
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/