1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-12 13:48:12 +09:00

Add relation types

This commit is contained in:
Aya Morisawa 2018-12-19 16:59:43 +09:00
parent 6ef83d9c59
commit 78ec06bda3
No known key found for this signature in database
GPG Key ID: 3E64865D70D579F2
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>;