paul rubin <[email protected]> added the comment:
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()))
----------
nosy: +phr
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44571>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com