def greduce(func, strm):
    '''    
    Reduce generator. 
    @param func: function of two arguments. Unlike reduce the function passed
                 into the generator may not need to return a value but modify
                 its first argument inplace like list.extend.
    @param strm: list of values passed into func. strm can be updated using 
                 greduce.send(l) where l is another list.
    '''
    res = None
    while 1:
        try:
            try:
                if res is None:
                    res, v = strm[:2]
                    del strm[0:2]
                else:
                    v = strm[0]
                    del strm[0]
            except (ValueError, IndexError):
                l = yield
            else:
                out = func(res, v)
                res = out if out is not None else res
                l = yield res
            if l:
                strm.extend(l)
                yield
        except YieldResult:
            yield res
    
    Reduce generator. 
    @param func: function of two arguments. Unlike reduce the function passed
                 into the generator may not need to return a value but modify
                 its first argument inplace like list.extend.
    @param strm: list of values passed into func. strm can be updated using 
                 greduce.send(l) where l is another list.
    
recipes/Python/498264_reduce_as_a_generator/recipe-498264.py