def apply_to_all_elements(x, fn):
    """
    Recursively apply function to all elements of nested lists/tuples
    """
    if type(x) not in (list, tuple):
        return fn(x)
    return [apply_to_all_elements(y, fn) for y in x]