Skip to content

Commit

Permalink
improve flatten performance by reducing copy
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 18, 2021
1 parent 1ba8b03 commit a1a2550
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion builtin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ def range($start; $end): _range($start; $end; 1);
def range($start; $end; $step): _range($start; $end; $step);

def _flatten($x):
reduce .[] as $i
( [];
if $i | type == "array" and $x != 0
then . + ($i | _flatten($x-1))
else . + [$i]
end);
map(if type == "array" and $x != 0 then _flatten($x - 1) else [.] end) | add;
def flatten($x):
if $x < 0
then error("flatten depth must not be negative")
Expand Down
4 changes: 4 additions & 0 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,11 @@
input: |
[0, [[[[[1]]]]]]
[0, [1, [2]], [1, [[3], 2]]]
{"x": [1], "y": [[2]], "z": [[[3]]]}
expected: |
[0,1]
[0,1,2,1,3,2]
[1,2,3]
- name: flatten/1 function
args:
Expand All @@ -1181,9 +1183,11 @@
input: |
[0, [[[[[1]]]]]]
[0, [1, [2]], [1, [[3], 2]]]
{"x": [1], "y": [[2]], "z": [[[3]]]}
expected: |
[0,[[[1]]]]
[0,1,2,1,[3],2]
[1,2,[3]]
- name: min, min_by, max, max_by functions
args:
Expand Down

0 comments on commit a1a2550

Please sign in to comment.