[issue18652] Add itertools.coalesce

2013-08-04 Thread Nick Coghlan

Nick Coghlan added the comment:

(Updated the issue title to reflect the currently proposed name and location 
for the functionality)

While I'm a fan of explicit iteration as well (inside every reduce is a loop 
trying to get out), I think the fact Martin's explicit loop is buggy (it will 
never match re2 as it always bails on the first iteration) helps make the case 
for coalesce. The correct explicit loop looks something like this:

for r in (re1, re2):
m = r.match('abc')
if m is None:
continue
if r is re1:
print('re1', m.group(1))
elif r is re2:
print('re1', m.group(1))
break # Matched something
else:
print('No match')

(Or the equivalent that indents the loop body further and avoids the continue 
statement)

The coalesce version has a definite advantage in not needing a loop else clause 
to handle the nothing matched case:

m = coalesce(regexp.match('abc') for regexp in [re1, re2])
if m is None:
print('no match!')
elif m.re is re1:
print('re1', m.group(1))
elif m.re is re2:
print('re2', m.group(1))

--
title: Add a “first”-like function to the stdlib - Add itertools.coalesce

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Martin v . Löwis

Martin v. Löwis added the comment:

Simplicity is in the eye of the beholder, yet... you need to remember the break 
when *writing* the code, so the loop might be more difficult to write (but 
then, I need to remember/lookup the function name and parameters for 
coalesce)... when reading the code, the break is already there, and easy to 
notice. With Nick's remark, it's even more obvious that it is difficult to 
write :-)

If an unknown (to the reader) function is used, reading the code becomes 
actually difficult, since the reader either needs to guess what the function 
does, or look it up.

Note that I'm not objecting the addition of the function (I'm neutral), just 
the claim that there are no simple alternatives. I'm neutral because it falls 
under the not every two-line function needs to go into the standard library 
rule; but then, if it is really popular, it helps readability if it is in the 
library (rather than duplicated by users).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Mark Dickinson

Mark Dickinson added the comment:

[Nick]
 Regarding the key parameter name, I believe this is closer to
 itertools.groupby ...

Isn't this mostly about the return type?  `pred` is an indication that a 
boolean is being returned (or that the return value will be interpreted in a 
boolean context), while `key` is used for more general transformations.  In 
that sense, `pred` makes more sense here.

--
nosy: +mark.dickinson

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Nick Coghlan

Nick Coghlan added the comment:

Mark's rationale makes sense to me. I believe that would make the
latest version of the proposed API (in the itertools module):

def coalesce(iterable, default=None, pred=None):
...

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

def coalesce(iterable, default=None, pred=None):
return next(filter(pred, iterable), default)

Are you sure you want add this one-line function to the itertools module rather 
then to recipes?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Hynek Schlawack

Hynek Schlawack added the comment:

 def coalesce(iterable, default=None, pred=None):
return next(filter(pred, iterable), default)
 
 Are you sure you want add this one-line function to the itertools module 
 rather then to recipes?

Well, for many – including me – it would mean to have this one-line function in 
every other project or a PyPI dependency.  I’m certain there are other short 
but useful functions in the stdlib.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread R. David Murray

R. David Murray added the comment:

I'm not going to object to the name, since I see that it is used elsewhere in 
programming for the proposed meaning.  But allow me to rant about the 
corruption of the English language here.  To me, coalesce should involve a 
computation based on all of the elements of a list, not just picking out the 
first non-false value.

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

 Well, for many – including me – it would mean to have this one-line function 
 in every other project or a PyPI dependency.

But why you want to have a separate function instead of just use two builtins?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Tim Peters

Tim Peters added the comment:

FWIW, I like this.  It would be a nice addition to the itertools module. +1

The `key` argument should be renamed to `pred`, as others have said.

As to the name, I like first_true.  Does what it says.  Plain first is 
misleading, and coalesce is both inscrutable and nearly impossible to spell 
;-)

--
nosy: +tim_one

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Hynek Schlawack

Hynek Schlawack added the comment:

 But why you want to have a separate function instead of just use two builtins?

This question has been answered twice now, once from Nick – please refer above.

It's a clunky and error-prone solution to a common problem. Maybe you can't 
emphasize because it's not a common problem to you but that doesn't make it 
less useful.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Nick Coghlan

Nick Coghlan added the comment:

A wild timbot appears! :)

Tim Peters added the comment:
 As to the name, I like first_true.  Does what it says.  Plain first
is misleading, and coalesce is both inscrutable and nearly impossible to
spell ;-)

A fair point, although skipping the underscore (firsttrue or nexttrue)
would arguably be more consistent with other itertools names.

I guess it's over to Raymond now as module maintainer for a yes/docs
recipe/no response, and if one of the first two, the exact name used.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18652] Add itertools.coalesce

2013-08-04 Thread Tim Peters

Tim Peters added the comment:

 skipping the underscore (firsttrue or nexttrue)
 would arguably be more consistent with other itertools names.

Like izip_longest and combinations_with_replacement? ;-)

I care about readability here more than consistency with the bulk of itertools 
names.  first ends with t and true begins with t, which makes 
firsttrue an eyesore.  first_true strikes me as very easy to read, spell, 
remember and understand.

Offhand I don't know what nexttrue is supposed to mean (like if that's 
supposed to mean the first true iterate, why didn't they say 'first' instead of 
'next'?).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18652
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com