def dict_map(f, d):
    """Apply function f to all terminal elements of dict d."""
    if isinstance(d, dict):
        return {k: dict_map(f, v) for k, v in d.items()}
    elif isinstance(d, list):
        return [dict_map(f, x) for x in d]
    else:
        return f(d)