[Python-ideas] Allow filter(items)
Shane Green
shane at umbrellacode.com
Wed Aug 7 09:42:47 CEST 2013
Yes, and that’s why “filtered” could be more significant that a second name for “filter”.
But, if we’re really methodical we should consider adding an in-place “filter(predicate=None)” method to list because having one seems makes seems to make sense for all the reasons having an in-place sort method does, with the exception of implementation details, and the new filtered() method that reflects list’s built but extends its API and returns an iterator, would make perfect sense being defined in __builtin__ right-along sorted.
On Aug 6, 2013, at 10:21 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130807/9ef9753e/attachment.html>
More information about the Python-ideas
mailing list