Repository navigation
Expand file tree
/
Copy pathArrayUtil.ts
More file actions
51 lines (45 loc) · 1.51 KB
/
Copy pathArrayUtil.ts
File metadata and controls
51 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { sample as _Sample } from "tstl/ranges/algorithm/random";
export namespace ArrayUtil
{
export async function asyncFilter<Input>
(
elements: Input[],
pred: (elem: Input, index: number, array: Input[]) => Promise<boolean>
): Promise<Input[]>
{
const ret: Input[] = [];
for (let i: number = 0; i < elements.length; ++i)
if (await pred(elements[i], i, elements) === true)
ret.push(elements[i]);
return ret;
}
export async function asyncMap<Input, Output>
(
elements: Input[],
closure: (elem: Input, index: number, array: Input[]) => Promise<Output>
): Promise<Output[]>
{
const ret: Output[] = [];
for (let i: number = 0; i < elements.length; ++i)
ret.push(await closure(elements[i], i, elements));
return ret;
}
export async function asyncRepeat<T>(count: number, closure: (index: number) => Promise<T>): Promise<T[]>
{
const ret: T[] = [];
for (let i: number = 0; i < count; ++i)
ret.push(await closure(i));
return ret;
}
export function has<T>(elements: T[], pred: (elem: T) => boolean): boolean
{
return elements.find(pred) !== undefined;
}
export function repeat<T>(count: number, closure: (index: number) => T): T[]
{
const ret: T[] = [];
for (let i: number = 0; i < count; ++i)
ret.push(closure(i));
return ret;
}
}