mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 11:53:09 +02:00
Add relation types
This commit is contained in:
parent
6ef83d9c59
commit
78ec06bda3
2 changed files with 6 additions and 1 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import { EndoRelation } from "./relation";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count the number of elements that satisfy the predicate
|
* Count the number of elements that satisfy the predicate
|
||||||
*/
|
*/
|
||||||
|
@ -61,7 +63,7 @@ export function maximum(xs: number[]): number {
|
||||||
* Splits an array based on the equivalence relation.
|
* Splits an array based on the equivalence relation.
|
||||||
* The concatenation of the result is equal to the argument.
|
* The concatenation of the result is equal to the argument.
|
||||||
*/
|
*/
|
||||||
export function groupBy<T>(f: (x: T, y: T) => boolean, xs: T[]): T[][] {
|
export function groupBy<T>(f: EndoRelation<T>, xs: T[]): T[][] {
|
||||||
const groups = [] as T[][];
|
const groups = [] as T[][];
|
||||||
for (const x of xs) {
|
for (const x of xs) {
|
||||||
if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) {
|
if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) {
|
||||||
|
|
3
src/prelude/relation.ts
Normal file
3
src/prelude/relation.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export type Relation<T, U> = (x: T, y: U) => boolean;
|
||||||
|
|
||||||
|
export type EndoRelation<T> = Relation<T, T>;
|
Loading…
Reference in a new issue