Fixed transpose for vectors and non-array lists

This commit is contained in:
Revar Desmera 2019-05-12 12:51:58 -07:00
parent 78e3cd3c27
commit dba2edc984
2 changed files with 2 additions and 3 deletions

View file

@ -496,8 +496,7 @@ function array_dim(v, depth=undef) =
// 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]];
is_list(arr[0])? [for (i=[0:len(arr[0])-1]) [for (j=[0:len(arr)-1]) arr[j][i]]] : arr;

View file

@ -215,7 +215,7 @@ test_array_dim();
module test_transpose() {
assert(transpose([[1,2,3],[4,5,6],[7,8,9]]) == [[1,4,7],[2,5,8],[3,6,9]]);
assert(transpose([[1,2,3],[4,5,6]]) == [[1,4],[2,5],[3,6]]);
assert(transpose([3,4,5]) == [[3],[4],[5]]);
assert(transpose([3,4,5]) == [3,4,5]);
}
test_transpose();