[Python-ideas] custom predicate for all() & any()

Wes Turner wes.turner at gmail.com
Tue Mar 29 14:32:53 EDT 2016


On Mar 29, 2016 12:49 PM, "Michael Selik" <mike at selik.org> wrote:
>
>
>
> On Tue, Mar 29, 2016 at 9:15 AM Sven R. Kunze <srkunze at mail.de> wrote:
>>
>> On 26.03.2016 18:04, Terry Reedy wrote:
>> > So proposed all(iterable, predicate) ==  all(map(predicate, iterable))
>>
>> I am no native English speaker. Is predicate the right word for this?
>
>
> I'm not sure being a native speaker would help in this case. :-)
>
> One reasonably common definition of "predicate": a function that tests a
condition on a value and returns True/False. Some folks define it more
broadly, but then the word loses its usefulness as distinct from other
kinds of functions. The usage of "predicate" here comes from predicate
logic, or more specifically the usage of the term in languages like Lisp.
Where Python says ``'42'.isnumeric()`` a lisper might say ``(numberp 42)``,
the "p" suffix indicating that it's a predicate function.
>
> https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node69.html
>
>>
>> I get the feeling we would rebuilding things over and over again: list
>> comprehensions, filter, map, itertools, for-loops etc.
>
>
> Certainly feels like it.

* "predicate" seems to be the common term
* (predicate, iterable) seems to be the, at least in Python, common
function signature

* itertools.ifilter (predicate, iterable)

   https://docs.python.org/2/library/itertools.html#itertools.ifilter

* python3: filter(function, iterable))

   https://docs.python.org/3/library/functions.html#filter

* toolz: *(predicate, iterable)

* funcy.ifilter(pred, seq)

   http://funcy.readthedocs.org/en/stable/seqs.html#transform-and-filter

... "iterable" / sequence

>
>>
>> I think the underlying issue is the number of parentheses involved. I
>> myself somehow avoid nesting too many function calls into one line just
>> because of that; not because of the complexity involved but because it
>> looks strange.
>
>
> Same here. But I don't mind the occasional throwaway variable. When it's
a multi-step process, a good name for an intermediate stage helps me think.

much easier to debug with e.g. ipdb, pdb, when there are intermediate
variables.

IDK if there are any less references to track without intermediate
variables?

>
>>
>> Of course the number of parentheses involved is an
>> indicator of the complexity. Not sure if there is another way of
>> handling this;

* git commit
* :PymodeLintAuto #vim
* git diff
* git commit

... or gg=G  #vim

> maybe piping or something. Would be great if we could
>> avoid writing ))) or ))))).
>
>
> http://xkcd.com/297/
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160329/4f554154/attachment.html>


More information about the Python-ideas mailing list