On Dec 5, 2019, at 08:53, Juancarlo Añez <apal...@gmail.com> wrote:
> 
> BTW, a common function in extensions to itertools is first():
> 
>     def first(seq, default=None):
>         return next(iter(seq), default= default)
> 
> That function, first(), would also be a nice addition in itertools, and 
> findfirst() could be implemented using it. first() avoids most use cases 
> needing to check if a sequence or iterator is empty before using a default 
> value. MHO is that first() deals with so many common cases that it should be 
> a builtin.

I think this was proposed for itertools and rejected. I don’t remember why, but 
generally there’s resistance to adding anything that you could write yourself 
(and are unlikely to get wrong) on top of itertools and builtins, unless it 
needs to loop and yield itself (in which case it might need the performance 
boost of iterating in C instead of Python), because that’s what the recipes are 
for. And I suppose if you see the recipe for nth you don’t learn anything from 
the recipe for first.

But people seem more open to recipes being “everything useful” rather than only 
“everything useful that also teaches an important idea”, and the recipe docs 
even link to more-itertools for people looking to use them out of the box (and 
first is in more-itertools). Also, I think it’s pretty clear that people often 
don’t think of first when they need it, so even if they could write it if they 
thought of it, they don’t because they don’t.

So maybe it’s worth at least adding first as a recipe, even if people don’t 
think it’s worth adding to the module itself?

(Personally, I use first if I’ve already imported more-itertools for something 
else, but otherwise I just next Iter.)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/H3GURL35C7AZ3ZBK6CQZGGCISUZ42WDV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to