[Python-ideas] Break the dominance of boolean values in boolean context
Terry Reedy
tjreedy at udel.edu
Thu Sep 15 04:52:47 CEST 2011
On 9/14/2011 9:38 PM, Nick Coghlan wrote:
> On Thu, Sep 15, 2011 at 10:20 AM, Chris Rebert<pyideas at rebertia.com> wrote:
>>> While such a function might be useful, I think it would be
>>> better provided as a new function called something like
>>> first(), rather than changing any().
>>
>> Ruby calls it "detect":
>> http://www.ruby-doc.org/core/classes/Enumerable.html#M001485
>
> from itertools import dropwhile
> def _not(x): return not x
>
> def first(iterable):
> return next(dropwhile(_not, iterable))
>
> Adjust implementation to taste/use case (e.g. make the predicate
> configurable or accept a default value rather than throwing
> StopIteration for empy sequences). There comes a point where it is
> better to just let people compose existing tools according to their
> specific needs rather than providing yet another special purpose tool.
That is what itertool is designed for. I believe chain(dropwhile(...),
[default]) will add a default.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list