from itertools import groupby


def group_by_and_transform(grouper, transformer, iterable):  # pragma: no cover
    """ Sort & Group iterable by grouper, apply transformer to each group.

        Grouper must be a function that takes an item in the iterable and
        returns a sort key.

        Returns a dictionary of group keys matched to lists.
        """

    return {key: map(transformer, group) for key, group in groupby(sorted(iterable, key=grouper), key=grouper)}