Message403623
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())) |
|
Date |
User |
Action |
Args |
2021-10-11 07:09:53 | phr | set | recipients:
+ phr, tim.peters, rhettinger, serhiy.storchaka, miss-islington, pavel-lexyr |
2021-10-11 07:09:53 | phr | set | messageid: <1633936193.16.0.405800849008.issue44571@roundup.psfhosted.org> |
2021-10-11 07:09:53 | phr | link | issue44571 messages |
2021-10-11 07:09:53 | phr | create | |
|