def average(v, state):
    """ This function is used to output a stream
    where the n-th value on the output stream is
    the average of the first n values of the
    input stream.
    
    The state of the input stream is the pair (n,cum).
    When used to create a stream, n is the number
    of values received on the input stream, and cum
    is the sum of all the values in the input stream
    received so far.
    v is the next value received in the input stream.
    
    """
    n, cum = state
    n += 1
    cum += v
    mean = cum/float(n)
    state = (n, cum)
    return (mean, state)