def combinations_with_replacement(iterable, r):
    """ This function acts as a replacement for the
        itertools.combinations_with_replacement function. The original does not
        replace items that come earlier in the provided iterator.
    """
    stk = [[i,] for i in iterable]
    pop = stk.pop
    while len(stk) > 0:
        top = pop()
        if len(top) == r:
            yield tuple(top)
        else:
            stk.extend(top + [i] for i in iterable)