(key, count, combine)
return conglomerate
Create a rule to conglomerate identical args

    >>> from sympy.strategies import glom
    >>> from sympy import Add
    >>> from sympy.abc import x

    >>> key     = lambda x: x.as_coeff_Mul()[1]
    >>> count   = lambda x: x.as_coeff_Mul()[0]
    >>> combine = lambda cnt, arg: cnt * arg
    >>> rl = glom(key, count, combine)

    >>> rl(Add(x, -x, 3*x, 2, 3, evaluate=False))
    3*x + 5

    Wait, how are key, count and combine supposed to work?
    >>> key(2*x)
    x
    >>> count(2*x)
    2
    >>> combine(2, x)
    2*x
Create a rule to conglomerate identical args
Create a rule to conglomerate identical args
def glom(key, count, combine):
    """ Create a rule to conglomerate identical args

    >>> from sympy.strategies import glom
    >>> from sympy import Add
    >>> from sympy.abc import x

    >>> key     = lambda x: x.as_coeff_Mul()[1]
    >>> count   = lambda x: x.as_coeff_Mul()[0]
    >>> combine = lambda cnt, arg: cnt * arg
    >>> rl = glom(key, count, combine)

    >>> rl(Add(x, -x, 3*x, 2, 3, evaluate=False))
    3*x + 5

    Wait, how are key, count and combine supposed to work?
    >>> key(2*x)
    x
    >>> count(2*x)
    2
    >>> combine(2, x)
    2*x
    """
    def conglomerate(expr):
        """ Conglomerate together identical args x + x -> 2x """
        groups = sift(expr.args, key)
        counts = dict((k, sum(map(count, args))) for k, args in groups.items())
        newargs = [combine(cnt, mat) for mat, cnt in counts.items()]
        if set(newargs) != set(expr.args):
            return new(type(expr), *newargs)
        else:
            return expr

    return conglomerate
def glom ( key , count , combine ) : def conglomerate ( expr ) : """ Conglomerate together identical args x + x -> 2x """ groups = sift ( expr . args , key ) counts = dict ( ( k , sum ( map ( count , args ) ) ) for k , args in groups . items ( ) ) newargs = [ combine ( cnt , mat ) for mat , cnt in counts . items ( ) ] if set ( newargs ) != set ( expr . args ) : return new ( type ( expr ) , * newargs ) else : return expr return conglomerate