diff options
| author | Aya Morisawa <AyaMorisawa4869@gmail.com> | 2018-12-19 17:08:09 +0900 |
|---|---|---|
| committer | Aya Morisawa <AyaMorisawa4869@gmail.com> | 2018-12-19 17:08:09 +0900 |
| commit | 8025b121af723581680f55ccb4b17d2dea9a8572 (patch) | |
| tree | 27f9b4ec3083bab83c2c49d710ded957cf76aa0d /src | |
| parent | Add relation types (diff) | |
| download | misskey-8025b121af723581680f55ccb4b17d2dea9a8572.tar.gz misskey-8025b121af723581680f55ccb4b17d2dea9a8572.tar.bz2 misskey-8025b121af723581680f55ccb4b17d2dea9a8572.zip | |
Add Predicate type
Diffstat (limited to 'src')
| -rw-r--r-- | src/prelude/array.ts | 7 | ||||
| -rw-r--r-- | 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 7eee87bf8b..778e5e37c3 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<T>(f: (x: T) => boolean, xs: T[]): number { + +export function countIf<T>(f: Predicate<T>, 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<T>(f: (x: T) => boolean, xs: T[]): T[] { +export function takeWhile<T>(f: Predicate<T>, 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 d3c3ee378a..b3aedc5ffc 100644 --- a/src/prelude/relation.ts +++ b/src/prelude/relation.ts @@ -1,3 +1,5 @@ +export type Predicate<T> = (x: T) => boolean; + export type Relation<T, U> = (x: T, y: U) => boolean; export type EndoRelation<T> = Relation<T, T>; |