Replace x with new if fn(x, old) is True.

    :param x: Any value
    :param old: The old value we want to replace
    :param new: The value replacing old
    :param fn: 
        The predicate function determining the relation between x and
        old. By default fn is the equality function.
    :returns: x or new

    >>> map(lambda x: replace(x, None, -1), [None, 1, 2, None])
    [-1, 1, 2, -1]

def replace(x, old, new, fn=operator.eq):
    """
    Replace x with new if fn(x, old) is True.

    :param x: Any value
    :param old: The old value we want to replace
    :param new: The value replacing old
    :param fn: 
        The predicate function determining the relation between x and
        old. By default fn is the equality function.
    :returns: x or new

    >>> map(lambda x: replace(x, None, -1), [None, 1, 2, None])
    [-1, 1, 2, -1]

    """
    return new if fn(x, old) else x