Created
April 27, 2026 07:37
-
-
Save vvgomes/7d40717255e360dfda6e8545c34575f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Matrix Transpose | |
| * | |
| * As a Ramda user, I'm aware the library already has a `transpose` function. | |
| * Still, I believe it is a fun exercise. | |
| * | |
| */ | |
| const { all, isEmpty, map, head, tail, concat } = require("ramda"); | |
| const transpose = (m) => | |
| all(isEmpty, m) | |
| ? [] | |
| : concat(Array(map(head, m)), transpose(map(tail, m))); | |
| describe("transpose", () => { | |
| const sampleInput = [ | |
| [1, 2, 3], | |
| [4, 5, 6], | |
| [7, 8, 9] | |
| ]; | |
| const expectedOutput = [ | |
| [1, 4, 7], | |
| [2, 5, 8], | |
| [3, 6, 9] | |
| ]; | |
| test("transpose a square matrix", () => { | |
| expect(transpose(sampleInput)).toEqual(expectedOutput); | |
| }); | |
| test("transpose a square matrix back", () => { | |
| expect(transpose(transpose(sampleInput))).toEqual(sampleInput); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment