From 78ec06bda3185896119378c851661322d0937a86 Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 19 Dec 2018 16:59:43 +0900 Subject: [PATCH] Add relation types --- src/prelude/array.ts | 4 +++- src/prelude/relation.ts | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 src/prelude/relation.ts diff --git a/src/prelude/array.ts b/src/prelude/array.ts index 21d18a0af4..7eee87bf8b 100644 --- a/src/prelude/array.ts +++ b/src/prelude/array.ts @@ -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(f: (x: T, y: T) => boolean, xs: T[]): T[][] { +export function groupBy(f: EndoRelation, xs: T[]): T[][] { const groups = [] as T[][]; for (const x of xs) { if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) { diff --git a/src/prelude/relation.ts b/src/prelude/relation.ts new file mode 100644 index 0000000000..d3c3ee378a --- /dev/null +++ b/src/prelude/relation.ts @@ -0,0 +1,3 @@ +export type Relation = (x: T, y: U) => boolean; + +export type EndoRelation = Relation;