def aggregate(gen):
    """
    Takes any iterable and aggregates subsequent values
    yielded by the iterable into a single value with a counter.

    Yields (value, count) pairs

    Will NOT handle None values correctly
    """
    current = None
    cnt = 0
    for item in gen:
        if item == current:
            cnt += 1
        else:
            if current is not None:
                yield current, cnt
            current = item
            cnt = 1
    if current is not None:
        yield current, cnt
