This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author phr
Recipients miss-islington, pavel-lexyr, phr, rhettinger, serhiy.storchaka, tim.peters
Date 2021-10-11.07:09:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1633936193.16.0.405800849008.issue44571@roundup.psfhosted.org>
In-reply-to
Content
Oh wow, before_and_after will go into the itertools module per that patch?  I found this issue while looking for a way to this, but had written the following implementation:

def span(pred, xs):
    # split xs into two iterators a,b where a() is the prefix of xs             
    # that satisfies the predicate, and b() is the rest of xs.                  
    # Similar to Data.List.Span in Haskell.                                     

    ixs = iter(xs)
    t = None
    def a():
        nonlocal t
        for x in ixs:
            if pred(x): yield x
            else: break
        t = x
    def b():
        return itertools.chain([t], ixs)
    return a, b

def tspan():  # test
    xs = [1,3,5,2,4,6,8]
    def odd(x): return x%2==1
    # This should print [1,3,5] then [2,4,6,8]                                  
    for p in span(odd, xs):
        print(list(p()))
History
Date User Action Args
2021-10-11 07:09:53phrsetrecipients: + phr, tim.peters, rhettinger, serhiy.storchaka, miss-islington, pavel-lexyr
2021-10-11 07:09:53phrsetmessageid: <1633936193.16.0.405800849008.issue44571@roundup.psfhosted.org>
2021-10-11 07:09:53phrlinkissue44571 messages
2021-10-11 07:09:53phrcreate