builtin.jq: simpler and faster transpose (#2758)
This commit is contained in:
1 parent
330559643a
commit
8f49600a6b
2 files changed
+5
-8
No files matched your search
+1
-8
@@ -181,14 +181,7 @@ def combinations(n):
|
|||||||
| combinations;
|
| combinations;
|
||||||
# transpose a possibly jagged matrix, quickly;
|
# transpose a possibly jagged matrix, quickly;
|
||||||
# rows are padded with nulls so the result is always rectangular.
|
# rows are padded with nulls so the result is always rectangular.
|
||||||
def transpose:
|
def transpose: [range(0; map(length)|max // 0) as $i | [.[][$i]]];
|
||||||
if . == [] then []
|
|
||||||
else . as $in
|
|
||||||
| (map(length) | max) as $max
|
|
||||||
| length as $length
|
|
||||||
| reduce range(0; $max) as $j
|
|
||||||
([]; . + [reduce range(0;$length) as $i ([]; . + [ $in[$i][$j] ] )] )
|
|
||||||
end;
|
|
||||||
def in(xs): . as $x | xs | has($x);
|
def in(xs): . as $x | xs | has($x);
|
||||||
def inside(xs): . as $x | xs | contains($x);
|
def inside(xs): . as $x | xs | contains($x);
|
||||||
def repeat(exp):
|
def repeat(exp):
|
||||||
|
|||||||
@@ -1487,6 +1487,10 @@ transpose
|
|||||||
[[1], [2,3]]
|
[[1], [2,3]]
|
||||||
[[1,2],[null,3]]
|
[[1,2],[null,3]]
|
||||||
|
|
||||||
|
transpose
|
||||||
|
[]
|
||||||
|
[]
|
||||||
|
|
||||||
ascii_upcase
|
ascii_upcase
|
||||||
"useful but not for é"
|
"useful but not for é"
|
||||||
"USEFUL BUT NOT FOR é"
|
"USEFUL BUT NOT FOR é"
|
||||||
|
|||||||
Reference in new issue
Block a user