def mean(values, empty=0):
    """ calculates mean of generator or iterator.
    Returns `empty` in case of an empty sequence """
    n, mean = 0, 0
    for value in values:
        n += 1
        mean += (value - mean) / n
    return mean if n > 0 else empty