-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelpers.ts
105 lines (89 loc) · 2.09 KB
/
helpers.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
export const removeAll = <X>(a: Array<X>, b: Array<X>): Array<X> => {
return a.filter((x) => {
return !b.includes(x)
})
}
export const even = (n: number) => {
return n === Math.floor(n / 2) * 2
}
export const le = (n1: number, n2: number) => {
return n1 <= n2
}
export const ge = (n1: number, n2: number) => {
return n1 >= n2
}
export const average = (ns: Array<number>) => {
let sum = 0;
for (const n of ns) {
sum += n;
}
return sum / ns.length;
}
export const filter = <X>(xs: Array<X>, f: (x: X) => boolean) => {
return xs.filter(f)
}
export const length = <X>(xs: Array<X>) => {
return xs.length
}
export const concat = (xs1: string, xs2: string) => {
return xs1 + xs2
}
export const removeDuplicates = <X>(xs: Array<X>): Array<X> => {
const ys: Array<X> = [];
for (const x of xs) {
if (!ys.includes(x)) {
ys.push(x)
}
}
return ys;
}
export const map = <X, Y>(xs: Array<X>, f: (x: X) => Y): Array<Y> => {
return xs.map(f)
}
export const range = (n: number) => {
return Array.from(Array(n).keys())
}
export const sample = <X>(xs: Array<X>, n: number) => {
const chosen: Set<number> = new Set()
const result: Array<X> = []
for (; n > 0; n--) {
let i: number;
while (true) {
i = Math.floor(Math.random() * xs.length)
if (!chosen.has(i)) {
chosen.add(i)
break
}
}
result.push(xs[i])
}
return result
}
export const fisherTest = (xs: Array<boolean>, ys: Array<boolean>) => {
const contigencyTable = [
[0, 0],
[0, 0],
]
const b2i = (b: boolean) => {
return b ? 1 : 0
}
for (let i = 0; i < xs.length; i++) {
contigencyTable[b2i(xs[i])][b2i(ys[i])]++
}
const factorial = (n: number): number => {
if (n === 0) {
return 1
} else {
return n * factorial(n - 1)
}
}
const [[a, b], [c, d]] = contigencyTable
const p = (factorial(a + b) * factorial(c + d) * factorial(a + c) * factorial(b + d)) / (factorial(a) * factorial(b) * factorial(c) * factorial(d) * factorial(a + b + c + d))
return p
}
export const startsWith = (s: string, prefix: string) => {
return s.startsWith(prefix)
}
export const colNameOfNumber = (n: number) => {
return n + ""
}