From 8f49600a6b717ada5973d518a9f042a4f2f8c58b Mon Sep 17 00:00:00 2001 From: pkoppstein Date: Mon, 24 Jul 2023 20:12:23 -0400 Subject: [PATCH] builtin.jq: simpler and faster transpose (#2758) --- src/builtin.jq | 9 +-------- tests/jq.test | 4 ++++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index a13d7845..45ba3060 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -181,14 +181,7 @@ def combinations(n): | combinations; # transpose a possibly jagged matrix, quickly; # rows are padded with nulls so the result is always rectangular. -def transpose: - 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 transpose: [range(0; map(length)|max // 0) as $i | [.[][$i]]]; def in(xs): . as $x | xs | has($x); def inside(xs): . as $x | xs | contains($x); def repeat(exp): diff --git a/tests/jq.test b/tests/jq.test index 4e0b950d..466d1850 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1487,6 +1487,10 @@ transpose [[1], [2,3]] [[1,2],[null,3]] +transpose +[] +[] + ascii_upcase "useful but not for é" "USEFUL BUT NOT FOR é"