This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
matrixZipMap.ts
72 lines (62 loc) · 1.75 KB
/
matrixZipMap.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
import * as R from 'ramda';
type Matrix<T> = T[][];
export function matrixMap<T1, T2, TR>(
m1: Matrix<T1>,
m2: Matrix<T2>,
cb: (d1: T1, d2: T2, i: number, j: number) => TR
) {
const mapMatrix = R.addIndex<T1[], TR[]>(R.map);
const mapRow = R.addIndex<T1, TR>(R.map);
return mapMatrix((iRow, i) =>
mapRow(
(ijValue, j) => cb(ijValue, m2[i][j], i, j),
iRow
), m1
);
}
export function matrixMap3<T1, T2, T3, TR>(
m1: Matrix<T1>,
m2: Matrix<T2>,
m3: Matrix<T3>,
cb: (d1: T1, d2: T2, d3: T3, i: number, j: number) => TR
) {
const mapMatrix = R.addIndex<T1[], TR[]>(R.map);
const mapRow = R.addIndex<T1, TR>(R.map);
return mapMatrix((iRow, i) =>
mapRow(
(ijValue, j) => cb(ijValue, m2[i][j], m3[i][j], i, j),
iRow
), m1
);
}
export function matrixMap4<T1, T2, T3, T4, TR>(
m1: Matrix<T1>,
m2: Matrix<T2>,
m3: Matrix<T3>,
m4: Matrix<T4>,
cb: (d1: T1, d2: T2, d3: T3, d4: T4, i: number, j: number) => TR
) {
const mapMatrix = R.addIndex<T1[], TR[]>(R.map);
const mapRow = R.addIndex<T1, TR>(R.map);
return mapMatrix((iRow, i) =>
mapRow(
(ijValue, j) => cb(ijValue, m2[i][j], m3[i][j], m4[i][j], i, j),
iRow
), m1
);
}
export function matrixMapN<TR>(
cb: (i: number, j: number, ...args: any[]) => TR,
...matrices: (any[][])[]
) {
const m1 = matrices.slice(0, 1);
const ms = matrices.slice(1);
const mapMatrix = R.addIndex<any[], TR[]>(R.map);
const mapRow = R.addIndex<any, TR>(R.map);
return mapMatrix((iRow, i) =>
mapRow(
(ijValue, j) => cb(i, j, [ijValue, ...ms.map(m => m[i][j])]),
iRow
), m1
);
}