Add Predicate type
This commit is contained in:
parent
78ec06bda3
commit
8025b121af
|
@ -1,9 +1,10 @@
|
||||||
import { EndoRelation } from "./relation";
|
import { EndoRelation, Predicate } from './relation';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count the number of elements that satisfy the predicate
|
* Count the number of elements that satisfy the predicate
|
||||||
*/
|
*/
|
||||||
export function countIf<T>(f: (x: T) => boolean, xs: T[]): number {
|
|
||||||
|
export function countIf<T>(f: Predicate<T>, xs: T[]): number {
|
||||||
return xs.filter(f).length;
|
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
|
* Returns the longest prefix of elements that satisfy the predicate
|
||||||
*/
|
*/
|
||||||
export function takeWhile<T>(f: (x: T) => boolean, xs: T[]): T[] {
|
export function takeWhile<T>(f: Predicate<T>, xs: T[]): T[] {
|
||||||
const ys = [];
|
const ys = [];
|
||||||
for (const x of xs) {
|
for (const x of xs) {
|
||||||
if (f(x)) {
|
if (f(x)) {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
export type Predicate<T> = (x: T) => boolean;
|
||||||
|
|
||||||
export type Relation<T, U> = (x: T, y: U) => boolean;
|
export type Relation<T, U> = (x: T, y: U) => boolean;
|
||||||
|
|
||||||
export type EndoRelation<T> = Relation<T, T>;
|
export type EndoRelation<T> = Relation<T, T>;
|
||||||
|
|
Loading…
Reference in New Issue