From 763f2e756acca565091bcb77b3e978b9161f87fc Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 19 Dec 2018 17:08:09 +0900 Subject: [PATCH] Add Predicate type --- src/prelude/array.ts | 7 ++++--- src/prelude/relation.ts | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/prelude/array.ts b/src/prelude/array.ts index 7eee87bf8..778e5e37c 100644 --- a/src/prelude/array.ts +++ b/src/prelude/array.ts @@ -1,9 +1,10 @@ -import { EndoRelation } from "./relation"; +import { EndoRelation, Predicate } from './relation'; /** * Count the number of elements that satisfy the predicate */ -export function countIf(f: (x: T) => boolean, xs: T[]): number { + +export function countIf(f: Predicate, xs: T[]): number { return xs.filter(f).length; } @@ -97,7 +98,7 @@ export function lessThan(xs: number[], ys: number[]): boolean { /** * Returns the longest prefix of elements that satisfy the predicate */ -export function takeWhile(f: (x: T) => boolean, xs: T[]): T[] { +export function takeWhile(f: Predicate, xs: T[]): T[] { const ys = []; for (const x of xs) { if (f(x)) { diff --git a/src/prelude/relation.ts b/src/prelude/relation.ts index d3c3ee378..b3aedc5ff 100644 --- a/src/prelude/relation.ts +++ b/src/prelude/relation.ts @@ -1,3 +1,5 @@ +export type Predicate = (x: T) => boolean; + export type Relation = (x: T, y: U) => boolean; export type EndoRelation = Relation;