From 590cba49b440bf6b079a6f3aa9f5aeca739cf4eb Mon Sep 17 00:00:00 2001 From: Evan Liomain Date: Sun, 13 Oct 2019 16:05:28 +0200 Subject: [PATCH 1/2] feat(mapmatrix): add mapMatrix function mapMatrix iterate over a 2D matrix, that is usefull to solve problem on 2D matrix --- .../__snapshots__/map-matrix.spec.ts.snap | 14 +++++++ src/map-matrix/index.ts | 1 + src/map-matrix/map-matrix.spec.ts | 40 +++++++++++++++++++ src/map-matrix/map-matrix.ts | 38 ++++++++++++++++++ src/taninsam.ts | 1 + 5 files changed, 94 insertions(+) create mode 100644 src/map-matrix/__snapshots__/map-matrix.spec.ts.snap create mode 100644 src/map-matrix/index.ts create mode 100644 src/map-matrix/map-matrix.spec.ts create mode 100644 src/map-matrix/map-matrix.ts diff --git a/src/map-matrix/__snapshots__/map-matrix.spec.ts.snap b/src/map-matrix/__snapshots__/map-matrix.spec.ts.snap new file mode 100644 index 000000000..e5920d1ce --- /dev/null +++ b/src/map-matrix/__snapshots__/map-matrix.spec.ts.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`mapMatrix function is a pure function 1`] = ` +Array [ + Array [ + 1, + 2, + ], + Array [ + 3, + 4, + ], +] +`; diff --git a/src/map-matrix/index.ts b/src/map-matrix/index.ts new file mode 100644 index 000000000..179ac78a9 --- /dev/null +++ b/src/map-matrix/index.ts @@ -0,0 +1 @@ +export * from './map-matrix'; diff --git a/src/map-matrix/map-matrix.spec.ts b/src/map-matrix/map-matrix.spec.ts new file mode 100644 index 000000000..e8331769e --- /dev/null +++ b/src/map-matrix/map-matrix.spec.ts @@ -0,0 +1,40 @@ +import { mapMatrix } from './map-matrix'; + +describe('mapMatrix function', () => { + const input = [[1, 2], [3, 4]]; + + test('is a pure function', () => { + mapMatrix(x => x)(input); + expect(input).toMatchSnapshot(); + }); + + test('[] |> mapMatrix === []', () => { + expect(mapMatrix(x => x)([])).toEqual([]); + }); + test('[[]] |> mapMatrix === [[]]', () => { + expect(mapMatrix(x => x)([[]])).toEqual([[]]); + }); + test('[[1, 4, 2], [1, 1, 3]] |> mapMatrix(x => 1 === x ? "." : "-") === [[".", "-", "-"], [".", ".", "-"]]', () => { + expect( + mapMatrix(x => (1 === x ? '.' : '-'))([[1, 4, 2], [1, 1, 3]]) + ).toEqual([['.', '-', '-'], ['.', '.', '-']]); + }); + + test('[[0, 0, 0], [0, 0, 0]] |> mapMatrix((element, x, y) => x + "-" + y) === [[".", "-", "-"], [".", ".", "-"]]', () => { + expect(mapMatrix((_, x, y) => `${x}-${y}`)([[0, 0, 0], [0, 0, 0]])) + .toMatchInlineSnapshot(` + Array [ + Array [ + "0-0", + "1-0", + "2-0", + ], + Array [ + "0-1", + "1-1", + "2-1", + ], + ] + `); + }); +}); diff --git a/src/map-matrix/map-matrix.ts b/src/map-matrix/map-matrix.ts new file mode 100644 index 000000000..a7ad3487e --- /dev/null +++ b/src/map-matrix/map-matrix.ts @@ -0,0 +1,38 @@ +import { map } from '../map'; + +/** + * @module matrix=>matrix + */ +/** + * Apply a function on each element of a matrix. + * @param iteratee The iteratee invoked per element. + * @return the new matrix + * @example + * ``` + * mapMatrix(x => 1 === x ? '.' : '-')([[1, 4, 2], [1, 1, 3]]) + * // [['.', '-', '-'], ['.', '.', '-']] + * ``` + * @example Using the chain + * ``` + * chain([[1, 4, 2], [1, 1, 3]]) + * .chain(mapMatrix(x => 1 === x ? '.' : '-')) + * .value() // [['.', '-', '-'], ['.', '.', '-']] + * ``` + */ +export function mapMatrix( + iteratee: ( + cell: T, + columnIndex?: number, + rowIndex?: number, + originalMatrix?: ReadonlyArray> + ) => U +): ( + matrix: ReadonlyArray> +) => ReadonlyArray> { + return matrix => + map, ReadonlyArray>((row, rowIndex) => + map((cell, columnIndex) => + iteratee(cell, columnIndex, rowIndex, matrix) + )(row) + )(matrix); +} diff --git a/src/taninsam.ts b/src/taninsam.ts index 9376a67d7..d09d63576 100644 --- a/src/taninsam.ts +++ b/src/taninsam.ts @@ -25,6 +25,7 @@ export * from './keys'; export * from './last'; export * from './length'; export * from './map'; +export * from './map-matrix'; export * from './max'; export * from './max-by'; export * from './min'; From 9ceca3cfda438825787995b6599f6df5934ddbbf Mon Sep 17 00:00:00 2001 From: Evan Liomain Date: Mon, 28 Oct 2019 08:05:21 +0100 Subject: [PATCH 2/2] test(map-matrix): fix unit test description --- src/map-matrix/map-matrix.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/map-matrix/map-matrix.spec.ts b/src/map-matrix/map-matrix.spec.ts index e8331769e..3483f67e1 100644 --- a/src/map-matrix/map-matrix.spec.ts +++ b/src/map-matrix/map-matrix.spec.ts @@ -20,7 +20,7 @@ describe('mapMatrix function', () => { ).toEqual([['.', '-', '-'], ['.', '.', '-']]); }); - test('[[0, 0, 0], [0, 0, 0]] |> mapMatrix((element, x, y) => x + "-" + y) === [[".", "-", "-"], [".", ".", "-"]]', () => { + test('[[0, 0, 0], [0, 0, 0]] |> mapMatrix((element, x, y) => x + "-" + y) === [["0-0", "1-0", "2-0",], ["0-1", "1-1", "2-1",]]', () => { expect(mapMatrix((_, x, y) => `${x}-${y}`)([[0, 0, 0], [0, 0, 0]])) .toMatchInlineSnapshot(` Array [