[Python-ideas] Break the dominance of boolean values in boolean context

Nick Coghlan ncoghlan at gmail.com
Thu Sep 15 03:38:13 CEST 2011


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.

Cheers,
Nick.

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



More information about the Python-ideas mailing list