matrix_transpose() generalized to transpose()

This commit is contained in:
Revar Desmera
2019-05-12 12:32:03 -07:00
parent a5065c1595
commit 78e3cd3c27
3 changed files with 34 additions and 21 deletions

View File

@@ -477,4 +477,29 @@ function array_dim(v, depth=undef) =
// Function: transpose()
// Description: Returns the transposition of the given array.
// Example:
// arr = [
// ["a", "b", "c"],
// ["d", "e", "f"],
// ["g", "h", "i"]
// ];
// t = transpose(arr);
// // Returns:
// // [
// // ["a", "d", "g"],
// // ["b", "e", "h"],
// // ["c", "f", "i"],
// // ]
// Example:
// transpose([3,4,5]); // Returns: [[3],[4],[5]]
function transpose(arr) =
arr==[]? [] :
is_list(arr[0])? [for (i=[0:len(arr[0])-1]) [for (j=[0:len(arr)-1]) arr[j][i]]] :
[for (x=arr) [x]];
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap