Make grouped_by a generic utility function.