Enhancing groupBy with Second Projection Function

From using ReadonlyArray.groupBy a lot, I think it could be useful to add a second projection function for the value:
export const groupByAndProject =
    <A, B>(fKey: (a: A) => string, fValue: (a: A) => B) =>
    (self: Iterable<A>): Record<string, ReadonlyArray.NonEmptyArray<B>> =>
        pipe(self, ReadonlyArray.groupBy(fKey), ReadonlyRecord.map(ReadonlyArray.map(fValue)));

That's because you usually don't need a second time the part of A that you used to make the key of the record. For instance, with the groupBy function as it is now, you would have:
declare const arr: ReadonlyArray<[key: string, value: number]>;

// Here, the same information now appears twice in the structure, once in the key of the record and once in the value
const groupBy1 = pipe(arr,ReadonlyArray.groupBy( ([key]) => key));
// Here the kay appears only once
const groupBy2 = pipe(arr,groupByAndProject(([key]) => key, ([_,value])=>value));
Was this page helpful?