'Internal helper: map values to keys and group by key. Here key is any
object derived from an input value by map_fn, and which can be grouped
by group_fn.
Args:
values: The values to be grouped by applying get_group(to_ref(value)).
map_fn: a function that maps a value to a key to be grouped.
group_fn: a function that groups the keys output by map_fn.
Returns:
A list where each element is a list of (key, index) pairs.  Here
index is the location of the value from which the key was derived in
the original list.'

def _map_and_group(self, values, map_fn, group_fn):

    indexed_key_groups = collections.defaultdict(list)
    for (index, value) in enumerate(values):
        key = map_fn(value)
        indexed_key_groups[group_fn(key)].append((key, index))
    return indexed_key_groups.values()
