Author ncoghlan
Recipients hynek, loewis, lukasz.langa, ncoghlan, rhettinger, serhiy.storchaka
Date 2013-08-04.12:09:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1375618185.88.0.0505194668165.issue18652@psf.upfronthosting.co.za>
In-reply-to
Content
(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))
History
Date User Action Args
2013-08-04 12:09:45ncoghlansetrecipients: + ncoghlan, loewis, rhettinger, lukasz.langa, hynek, serhiy.storchaka
2013-08-04 12:09:45ncoghlansetmessageid: <1375618185.88.0.0505194668165.issue18652@psf.upfronthosting.co.za>
2013-08-04 12:09:45ncoghlanlinkissue18652 messages
2013-08-04 12:09:45ncoghlancreate