def eliminate_error(n, s): """Eliminate error term of given asymptotic order n. The stream s must be based on halving h at each step for the formula used here to work.""" while True: a, b, s = unpack(2, s, k=1) yield (b * 2**n - a) / (2**(n - 1))
def len_gt(k, s): # see also unpythonic.slicing.islice; could implement as return islice(s)[k + 1] a, _ = unpack(1, drop(k, s)) return a # None if no item
def euler_transform(s): while True: a, b, c, s = unpack(3, s, k=1) yield c - ((c - b)**2 / (a - 2 * b + c))
def order(s): """Estimate asymptotic order of s, consuming the first three terms.""" a, b, c, _ = unpack(3, s) return round(log2(abs((a - c) / (b - c)) - 1))
def within(eps, s): # in real code, prefer unpythonic.it.within while True: # unpack with peek (but be careful, the rewinded tail is a tee'd copy) a, b, s = unpack(2, s, k=1) if abs(a - b) < eps: return b
def len_gt(k, s): a, _ = unpack(1, drop(k, s)) return a # None if no item
def within(eps, s): while True: # unpack with peek (but be careful, the rewinded tail is a tee'd copy) a, b, s = unpack(2, s, k=1) if abs(a - b) < eps: return b