Add relation types

This commit is contained in:
Aya Morisawa 2018-12-19 16:59:43 +09:00
parent 656b1b9486
commit e946eda05c
2 changed files with 6 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import { EndoRelation } from "./relation";
/**
* 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.
* 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[][];
for (const x of xs) {
if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) {

3
src/prelude/relation.ts Normal file
View File

@ -0,0 +1,3 @@
export type Relation<T, U> = (x: T, y: U) => boolean;
export type EndoRelation<T> = Relation<T, T>;