2 * Groups elements in an array by a provided comparison function.
3 * E.g. `[1, 1, 2, 3, 3] => [[1, 1], [2], [3, 3]]`
5 const groupWith = <T>(compare: (a: T, b: T) => boolean, arr: T[] = []) => {
6 const { groups } = arr.reduce<{ groups: T[][]; remaining: T[] }>(
8 const { groups, remaining } = acc;
9 const group = remaining.filter((b) => compare(a, b));
12 acc.groups = [...groups, group];
13 acc.remaining = remaining.filter((b) => !compare(a, b));
19 { groups: [], remaining: arr }
24 export default groupWith;