Message194355
(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)) |
|
Date |
User |
Action |
Args |
2013-08-04 12:09:45 | ncoghlan | set | recipients:
+ ncoghlan, loewis, rhettinger, lukasz.langa, hynek, serhiy.storchaka |
2013-08-04 12:09:45 | ncoghlan | set | messageid: <1375618185.88.0.0505194668165.issue18652@psf.upfronthosting.co.za> |
2013-08-04 12:09:45 | ncoghlan | link | issue18652 messages |
2013-08-04 12:09:45 | ncoghlan | create | |
|