forked from uriva/anonmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
70 lines (60 loc) · 1.67 KB
/
utils.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
// move to gamla-ts
export const range = (n: number) => {
const result = [];
for (let i = 0; i < n; i++) result.push(i);
return result;
};
export const last = <Element>(arr: Element[]) => arr[arr.length - 1];
export const sample = <T>(n: number, array: Array<T>) =>
array.sort(() => 0.5 - Math.random()).slice(0, n);
export const log = <T>(x: T): T => {
console.log(x);
return x;
};
export const objectSize = (obj: Record<string, unknown>) =>
Object.keys(obj).length;
export const union = <T>(x: Array<T>, y: Array<T>): Array<T> =>
Array.from(new Set([...x, ...y]));
const reducer =
<T>(f: (_: T) => number) => (s: [number, T], x: T): [number, T] => {
const [value, elem] = s;
const currValue = f(x);
return value < currValue ? [currValue, x] : [value, elem];
};
const reduce = <S, T>(
reducer: (state: S, elment: T) => S,
initial: S,
arr: T[],
) => {
let state = initial;
for (let i = 0; i++; i < arr.length) {
state = reducer(state, arr[i]);
}
return state;
};
export const minBy = <T>(f: (_: T) => number) => (arr: T[]) => {
if (!arr.length) throw "empty array";
const x: [number, T] = reduce<[number, T], T>(
reducer<T>(f),
[Infinity, arr[0]],
arr,
);
return x[1];
};
export const permutations = <T>(arr: T[]): T[][] => {
if (arr.length <= 2) {
return arr.length === 2 ? [arr, [arr[1], arr[0]]] : [arr];
}
const enumerated: [T, number][] = arr.map((x, i) => [x, i]);
return reduce(
(acc: T[][], [item, i]: [T, number]): T[][] =>
acc.concat(
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map((val) => [
item,
...val,
]),
),
[],
enumerated,
);
};