-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericArray.ts
More file actions
87 lines (68 loc) · 2.86 KB
/
GenericArray.ts
File metadata and controls
87 lines (68 loc) · 2.86 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { construct_simple_generic_procedure } from "generic-handler/GenericProcedure"
// only need to extend this four method to support map over custom array type
export const get_element = construct_simple_generic_procedure("get_element", 2,
<T>(array: T[], index: number): T => array[index]
)
export const set_element = construct_simple_generic_procedure("set_element", 3,
<T>(array: T[], index: number, value: T): T[] => {
const newArray = [...array];
newArray[index] = value;
return newArray;
}
)
export const get_length = construct_simple_generic_procedure("get_length", 1,
<T>(array: T[]): number => array.length
)
export const isArray = construct_simple_generic_procedure("isArray", 1,
<T>(obj: any): obj is T[] => Array.isArray(obj)
)
export const push = construct_simple_generic_procedure("push", 2, <T>(array: T[], item: T): T[] =>
set_element(array, get_length(array), item))
export const slice = construct_simple_generic_procedure("slice", 3, <T>(array: T[], start: number, end: number = get_length(array)): T[] => {
let result: T[] = [];
for (let i = start; i < end && i < get_length(array); i++) {
result = push(result, get_element(array, i));
}
return result;
}
)
export const filter = construct_simple_generic_procedure("filter", 2, <T>(array: T[], predicate: (item: T) => boolean): T[] => {
let result: T[] = [];
for (let i = 0; i < get_length(array); i++) {
const item = get_element(array, i);
if (predicate(item)) {
result = push(result, item);
}
}
return result;
})
export const map = construct_simple_generic_procedure("map", 2, <T, U>(array: T[], mapper: (item: T) => U): U[] => {
let result: U[] = [];
for (let i = 0; i < get_length(array); i++) {
result = push(result, mapper(get_element(array, i)));
}
return result;
})
export const reduce = construct_simple_generic_procedure("reduce", 3, <T, U>(array: T[], reducer: (acc: U, item: T) => U, initial: U): U => {
let acc = initial;
for (let i = 0; i < get_length(array); i++) {
acc = reducer(acc, get_element(array, i));
}
return acc;
})
export const first = <T>(array: T[]): T => get_element(array, 0)
export const rest = <T>(array: T[]): T[] => slice(array, 1, get_length(array))
export const second = <T>(array: T[]): T => get_element(array, 1)
export const third = <T>(array: T[]): T => get_element(array, 2)
export const construct = <T>(item: T, ...rest: T[]): T[] => {
let result: T[] = [item];
for (let i = 0; i < get_length(rest); i++) {
result = push(result, get_element(rest, i));
}
return result;
}
export const isPair = <T>(array: T[]): boolean =>
isArray(array) && get_length(array) > 0
export const isEmpty = <T>(array: T[]): boolean =>
get_length(array) === 0
export const isOriginalArray = isArray