def count_groups(generator):
    """
    Count the number of distinct elements in the stream, assuming that the
    elements come in groups.

    For example:
      >>> each(1, 2, 1, 10, 10, 5, 5) >> count_groups()
      5
    """
    # Instead of using a sentinel value, just look at the first value output by
    # the generator, and catch the exception if it is empty.
    try:
        last_element = next(generator)
        num_groups = 1
    except StopIteration:
        return 0

    for element in generator:
        if element != last_element:
            num_groups += 1
            last_element = element
    return num_groups