Created
October 30, 2015 14:58
-
-
Save CrossEye/320443d93afbddcac175 to your computer and use it in GitHub Desktop.
Simple matrix transposition
This file contains 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
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