def apply(f, *args):
    """
    Applies a function to sequence values or dicts of values.

    Args:
        f: The function to apply to ``x`` or all items in ``x``.
        *args: Sequence of arguments to be supplied to ``f``. If all arguments
            are dicts, the function ``f`` is applied key-wise to all elements
            in the dict. Otherwise the function is applied to all provided
            argument.s

    Return:
        ``{k: f(x_1[k], x_1[k], ...) for k in x}`` or ``f(x)`` depending on
        whether ``x_1, ...`` are a dicts or not.
    """
    if any(isinstance(x, dict) for x in args):
        results = {}
        d = [x for x in args if isinstance(x, dict)][0]
        for k in d:
            args_k = [arg[k] if isinstance(arg, dict) else arg
                      for arg in args]
            results[k] = f(*args_k)
        return results
    return f(*args)