[Python-ideas] Allow filter(items)

Nick Coghlan ncoghlan at gmail.com
Wed Aug 7 07:21:47 CEST 2013


On 7 August 2013 12:05, Haoyi Li <haoyi.sg at gmail.com> wrote:
>> I agree. I don't use filter very often and when I do I always have to
> think carefully about the order of the arguments. I'd prefer it if it
> were more like sort etc.
>
> OTOH, map filter and reduce all have a nice symmetry in thing(func, list). I
> guess the logic is that the sort predicate is optional, and the func for
> these other things isn't, but anyway...
>
> Boo for inconsistent argument orders =(

Right, the signatures of map, filter and functools.reduce all date
from a time before iterators became such a key language feature.

To switch from their functional forms to iterator focused equivalents,
you might leave map alone and define revised filtering and reduction
operations:

    def filtered(iterable, pred=None):
        """Filter out false values from an iterable. Accepts an
optional predicate function."""
        ...

    def reduced(start, iterable, op):
        """Reduces an iterable to a single value, given a start value
and binary operator."""
        ...

These might make better candidates for itertools inclusion than the
proposed "next_true", since they take the current functional APIs and
redesign them to be iterator focused.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list