mirror of
https://github.com/kokonect-link/cherrypick
synced 2024-12-14 14:48:53 +09:00
Add relation types
This commit is contained in:
parent
6ef83d9c59
commit
78ec06bda3
@ -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 New Issue
Block a user