[Python-ideas] Reduce/fold and scan with generator expressions and comprehensions

David Mertz mertz at gnosis.cx
Sun Oct 23 11:37:07 EDT 2016


Of course. But if you want last(), why not just spell the utility function
as I did? I.e. as a function:

    def last(it):
         for item in it:
             pass
        return item

That works fine for any iteratable (including a list, array, etc), whether
or not it's a reduction/accumulation.

On Oct 23, 2016 8:29 AM, "Danilo J. S. Bellini" <danilo.bellini at gmail.com>
wrote:

> What is `last(inf_iter)`. E.g `last(count())`.
>
> The "last" is just a helper function that gets the last value of an
> iterable. On sequences, it can be written to get the item at index -1 to
> avoid traversing it. Using it on endless iterables makes no sense.
>
> This makes it clear that is the users job to make sure `it` terminates.
>
> If one call "last" for something that doesn't terminate, an "endless"
> iterable, well, it's pretty obvious that it won't "end" nicely. It's not
> the Python job to solve the Entscheidungsproblem. If you call "sorted" on
> endless iterables, it would behave like "last", doesn't it?
>
> The whole point of this idea is the scan as a generator expression or
> list/set comprehension that can access the previous iteration output.
> Reduce/fold is just the last value of a scan, and the scan is still defined
> when there's no "last value".
>
> --
> Danilo J. S. Bellini
> ---------------
> "*It is not our business to set up prohibitions, but to arrive at
> conventions.*" (R. Carnap)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161023/44a8c0d0/attachment.html>


More information about the Python-ideas mailing list