def groupby(function, sequence):
    """

    Example:

    >>> from m2py import functional as f
    >>> f.groupby(len, ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank'])
    {3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}
    >>>
    >>>
    >>> f.groupby(f.is_even, [1, 2, 3, 4, 5, 6, 7])
    {False: [1, 3, 5, 7], True: [2, 4, 6]}
    >>>
    """

    output = {}

    for x in sequence:
        y = function(x)

        if not output.get(y):
            output[y] = [x]
        else:
            output[y].append(x)

    return output