Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Created April 27, 2026 07:37
Show Gist options
  • Select an option

  • Save vvgomes/7d40717255e360dfda6e8545c34575f9 to your computer and use it in GitHub Desktop.

Select an option

Save vvgomes/7d40717255e360dfda6e8545c34575f9 to your computer and use it in GitHub Desktop.
/*
* 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