def fields(d, names, pred=lambda x: True,
           key_transform=lambda x: x, value_transform=lambda x: x):
    """Returns the entries in this dictionary with keys appearing in names.

    :type d: dict
    :type names: [a]
    :param pred: a filter that is applied to the values of the dictionary.
    :type pred: (a -> bool)
    :param key_transform: a transform to apply to the key before returning it
    :type key_transform: a -> a
    :param value_transform: a transform to apply to the value before
        returning it
    :type value_transform: a -> a
    :rtype: dict
    """

    return dict((key_transform(k), value_transform(v))
                for k, v in d.items()
                if k in names and pred(v))