def repeat(x, n=None):
    """
    Returns a generator that indefinitly yields ``x`` (or ``n`` times if ``n``
    is supplied).
    """
    if n is None:
        n = -1
    elif n < 0:
        n = 0
    while n != 0:
        yield x
        n -= 1
