def group_by(fn, col):
    """
    Groups a collection according to the given *fn* into a dictionary.

    *fn* should return a hashable.
    """
    groups = {}
    for e in col:
        key = fn(e)
        subcol = groups.get(key, None)
        if subcol is None:
            groups[key] = [e]
        else:
            subcol.append(e)
    return groups