def flatten (d, *keys):
    """Flattens the dictionary d by merging keys in order such that later
    keys take precedence over earlier keys.

    """
    flat = { }

    for k in keys:
        flat = merge(flat, d.pop(k, { }))

    return flat