Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Add a generic, simple reduce/merge deep addition of object with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Aug 9, 2014
1 parent eba46d3 commit a21194e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/util/reduce-merge-deep-add.sh
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -e

read -d '' reduceMerge <<-'EOF' || true
def deepAdd(item):
item as $item
| with_entries(
($item[.key]) as $other
| if (.value | type) == "number" and ($other | type) == "number" then
.value += $other
else
.value |= deepAdd($other)
end
);
reduce .[1:][] as $halfway
(
.[0];
deepAdd($halfway)
)
EOF

cat | jq --slurp "$reduceMerge"

0 comments on commit a21194e

Please sign in to comment.