def _apply_nested(structure, fn):
    """Recursively apply a function to the elements of a list/tuple/dict."""
    if isinstance(structure, list):
        return list(_apply_nested(elem, fn) for elem in structure)
    elif isinstance(structure, tuple):
        return tuple(_apply_nested(elem, fn) for elem in structure)
    elif isinstance(structure, dict):
        return dict([(k, _apply_nested(v, fn))
                     for k, v in structure.items()])
    else:
        return fn(structure)