def filter_deltas(deltas, *, retain):
    """
    Example:
        >>> filter_deltas([{'deltas': {'foo': 2, 'bar': 1}}, {'deltas': {'foo': 5}}],
        ...               retain={'foo': lambda x: abs(x) > 3})
        [{'deltas': {'bar': 1}}, {'deltas': {'foo': 5}}]
        >>> filter_deltas([{'deltas': {'foo': 2}}, {'deltas': {'foo': 5}}],
        ...               retain={'bar': lambda x: False})
        [{'deltas': {'foo': 2}}, {'deltas': {'foo': 5}}]
    """
    filtered = (
        {**d,
         'deltas': {k: v for k, v in d['deltas'].items()
                    if retain.get(k, lambda x: True)(v)}}
        for d in deltas
    )

    return [d for d in filtered if d['deltas']]