Skip to content

Instantly share code, notes, and snippets.

@CrossEye
Created October 30, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrossEye/320443d93afbddcac175 to your computer and use it in GitHub Desktop.
Save CrossEye/320443d93afbddcac175 to your computer and use it in GitHub Desktop.
Simple matrix transposition
const R = require('ramda');
const mapIndexed = R.addIndex(R.map);
// transpose :: [[a]] -> [[a]] -- (row, col) becomes (col, row)
const transpose = function(matrix) {
return mapIndexed(function (_, col) {
return R.reject(R.isNil, R.map(R.prop(col), matrix));
}, R.head(matrix));
};
// transpose([[1, 2, 3], [4, 5, 6], [7. 8]]); //=> [[1, 4, 7], [2, 5, 8], [3, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment