Skip to content
Cannot retrieve contributors at this time
35 lines (26 sloc) 824 Bytes
from itertools import count, repeat, cycle, islice, tee, repeat, accumulate, chain
head = next
def tail(it):
return it
def take(n, it):
return [x for x in islice(it, n)]
def drop(n, it):
return islice(it, n, None)
def iterate(f, x):
"""return (x, f(x), f(f(x)), ...)"""
return accumulate(repeat(x), lambda fx, _: f(fx))
def until_convergence(it):
"""returns elements of it until the same element appears twice in a row,
then stops"""
def no_repeat(prev, curr):
if prev == curr: raise StopIteration
else: return curr
return accumulate(it, no_repeat)
def within_tolerance(tol, prev, curr):
if abs(prev - curr) < tol:
raise StopIteration
return curr
def until_nearly_convergence(it, tolerance=0.001):
return accumulate(it, partial(within_tolerance, tolerance))