Skip to content

Commit

Permalink
refactor: say bye to the weird groupBy friends (#13975)
Browse files Browse the repository at this point in the history
* refactor(frontend): say bye to the weird groupBy friends

* refactor(backend): say bye to the weird groupBy friends
  • Loading branch information
KisaragiEffective authored Jun 22, 2024
1 parent 7c22a64 commit 9368eb3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 76 deletions.
38 changes: 0 additions & 38 deletions packages/backend/src/misc/prelude/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,6 @@ export function maximum(xs: number[]): number {
return Math.max(...xs);
}

/**
* Splits an array based on the equivalence relation.
* The concatenation of the result is equal to the argument.
*/
export function groupBy<T>(f: EndoRelation<T>, xs: T[]): T[][] {
const groups = [] as T[][];
for (const x of xs) {
const lastGroup = groups.at(-1);
if (lastGroup !== undefined && f(lastGroup[0], x)) {
lastGroup.push(x);
} else {
groups.push([x]);
}
}
return groups;
}

/**
* Splits an array based on the equivalence relation induced by the function.
* The concatenation of the result is equal to the argument.
*/
export function groupOn<T, S>(f: (x: T) => S, xs: T[]): T[][] {
return groupBy((a, b) => f(a) === f(b), xs);
}

export function groupByX<T>(collections: T[], keySelector: (x: T) => string) {
return collections.reduce((obj: Record<string, T[]>, item: T) => {
const key = keySelector(item);
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
obj[key] = [];
}

obj[key].push(item);

return obj;
}, {});
}

/**
* Compare two arrays by lexicographical order
*/
Expand Down
38 changes: 0 additions & 38 deletions packages/frontend/src/scripts/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,6 @@ export function maximum(xs: number[]): number {
return Math.max(...xs);
}

/**
* Splits an array based on the equivalence relation.
* The concatenation of the result is equal to the argument.
*/
export function groupBy<T>(f: EndoRelation<T>, xs: T[]): T[][] {
const groups = [] as T[][];
for (const x of xs) {
const lastGroup = groups.at(-1);
if (lastGroup !== undefined && f(lastGroup[0], x)) {
lastGroup.push(x);
} else {
groups.push([x]);
}
}
return groups;
}

/**
* Splits an array based on the equivalence relation induced by the function.
* The concatenation of the result is equal to the argument.
*/
export function groupOn<T, S>(f: (x: T) => S, xs: T[]): T[][] {
return groupBy((a, b) => f(a) === f(b), xs);
}

export function groupByX<T>(collections: T[], keySelector: (x: T) => string) {
return collections.reduce((obj: Record<string, T[]>, item: T) => {
const key = keySelector(item);
if (typeof obj[key] === 'undefined') {
obj[key] = [];
}

obj[key].push(item);

return obj;
}, {});
}

/**
* Compare two arrays by lexicographical order
*/
Expand Down

0 comments on commit 9368eb3

Please sign in to comment.