Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/map-matrix/__snapshots__/map-matrix.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
],
]
`;
1 change: 1 addition & 0 deletions src/map-matrix/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './map-matrix';
40 changes: 40 additions & 0 deletions src/map-matrix/map-matrix.spec.ts
Original file line number Diff line number Diff line change
@@ -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) === [["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 [
Array [
"0-0",
"1-0",
"2-0",
],
Array [
"0-1",
"1-1",
"2-1",
],
]
`);
});
});
38 changes: 38 additions & 0 deletions src/map-matrix/map-matrix.ts
Original file line number Diff line number Diff line change
@@ -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<T, U>(
iteratee: (
cell: T,
columnIndex?: number,
rowIndex?: number,
originalMatrix?: ReadonlyArray<ReadonlyArray<T>>
) => U
): (
matrix: ReadonlyArray<ReadonlyArray<T>>
) => ReadonlyArray<ReadonlyArray<U>> {
return matrix =>
map<ReadonlyArray<T>, ReadonlyArray<U>>((row, rowIndex) =>
map<T, U>((cell, columnIndex) =>
iteratee(cell, columnIndex, rowIndex, matrix)
)(row)
)(matrix);
}
1 change: 1 addition & 0 deletions src/taninsam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down