{"action":"created","issue":{"url":"https://api.github.com/repos/stedolan/jq/issues/450","labels_url":"https://api.github.com/repos/stedolan/jq/issues/450/labels{/name}","comments_url":"https://api.github.com/repos/stedolan/jq/issues/450/comments","events_url":"https://api.github.com/repos/stedolan/jq/issues/450/events","html_url":"https://github.com/stedolan/jq/pull/450","id":36643172,"number":450,"title":"Add jq builtin reduce_keys(f)","user":{"login":"XavM","id":1206754,"avatar_url":"https://avatars.githubusercontent.com/u/1206754?v=3","gravatar_id":"","url":"https://api.github.com/users/XavM","html_url":"https://github.com/XavM","followers_url":"https://api.github.com/users/XavM/followers","following_url":"https://api.github.com/users/XavM/following{/other_user}","gists_url":"https://api.github.com/users/XavM/gists{/gist_id}","starred_url":"https://api.github.com/users/XavM/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/XavM/subscriptions","organizations_url":"https://api.github.com/users/XavM/orgs","repos_url":"https://api.github.com/users/XavM/repos","events_url":"https://api.github.com/users/XavM/events{/privacy}","received_events_url":"https://api.github.com/users/XavM/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/stedolan/jq/labels/feature%20request:%20jq%20function%20library","name":"feature request: jq function library","color":"207de5"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2014-06-27T07:57:09Z","updated_at":"2015-09-17T05:47:32Z","closed_at":"2015-07-25T03:56:24Z","pull_request":{"url":"https://api.github.com/repos/stedolan/jq/pulls/450","html_url":"https://github.com/stedolan/jq/pull/450","diff_url":"https://github.com/stedolan/jq/pull/450.diff","patch_url":"https://github.com/stedolan/jq/pull/450.patch"},"body":"reduce_keys(.foo) takes as input an array of objects, and produces a single object with values added for a same key \r\n\r\nExample :\r\n\r\n        jq 'reduce_keys(.)'\r\nInput\t[{\"test-a\": 1}, {\"test-a\": 2},{\"test-a\": 3},{\"test-b\": 4},{\"test-b\": 5}]\r\nOutput\t{\"test-a\":6,\"test-b\":9}\r\n\r\n        jq 'reduce_keys(.)'\r\nInput\t[{\"test-a\": [\"a\"]}, {\"test-a\": [\"b\"]},{\"test-a\": [\"c\"]},{\"test-b\": [\"x\"]},{\"test-b\": [\"y\"]}]\r\nOutput\t{\"test-a\":[\"a\",\"b\",\"c\"],\"test-b\":[\"x\",\"y\"]}\r\n\r\n        jq 'reduce_keys(.)'\r\nInput\t[{\"test-a\": \"a\"}, {\"test-a\": \"b\"},{\"test-a\": \"c\"},{\"test-b\": \"x\"},{\"test-b\": \"y\"}]\r\nOutput\t{\"test-a\":\"abc\",\"test-b\":\"xy\"}\r\n"},"comment":{"url":"https://api.github.com/repos/stedolan/jq/issues/comments/140975823","html_url":"https://github.com/stedolan/jq/pull/450#issuecomment-140975823","issue_url":"https://api.github.com/repos/stedolan/jq/issues/450","id":140975823,"user":{"login":"pkoppstein","id":172847,"avatar_url":"https://avatars.githubusercontent.com/u/172847?v=3","gravatar_id":"","url":"https://api.github.com/users/pkoppstein","html_url":"https://github.com/pkoppstein","followers_url":"https://api.github.com/users/pkoppstein/followers","following_url":"https://api.github.com/users/pkoppstein/following{/other_user}","gists_url":"https://api.github.com/users/pkoppstein/gists{/gist_id}","starred_url":"https://api.github.com/users/pkoppstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pkoppstein/subscriptions","organizations_url":"https://api.github.com/users/pkoppstein/orgs","repos_url":"https://api.github.com/users/pkoppstein/repos","events_url":"https://api.github.com/users/pkoppstein/events{/privacy}","received_events_url":"https://api.github.com/users/pkoppstein/received_events","type":"User","site_admin":false},"created_at":"2015-09-17T05:47:32Z","updated_at":"2015-09-17T05:47:32Z","body":"@jnothman - Personally, I'd like to see a \"summation\" function added to jq's builtins, using the following definition: \r\n```\r\ndef summation( f ): reduce .[] as $o (0; . + ($o | f )) ;\r\n````\r\nThis of course corresponds very closely to the mathematical SIGMA and I'd probably suggest the name \"SIGMA\" except that there are no all-uppercase names in the collection of jq builtins.  (There were objections to using the name \"sigma\" on the grounds that it could refer to the population standard error.)\r\n\r\nsummation/1 as defined immediately above is short but amazingly versatile, courtesy of the versatility of `.[]` (which works on arrays and objects), of `+` (which works on numbers and objects), and of jq itself. It should also be noted that sigma/1 yields efficient solutions very easily.  It does privilege \"+\", but in only in the same way as \"add\".\r\n\r\nAs an example of the usefulness of summation/1, here is a jq function to compute the correlation coefficient of two arrays (efficiently and fairly robustly):\r\n```\r\n# correlation coefficient - avoiding overflow\r\ndef r(f; g):\r\n  length as $n\r\n  | (. | summation(f)/$n) as $af\r\n  | (. | summation(g)/$n) as $ag\r\n  | (. | summation( (f - $af) * (g - $ag))) as $prod \r\n  | (. | summation( (f - $af) * (f - $af))) as $ssf\r\n  | (. | summation( (g - $ag) * (g - $ag))) as $ssg\r\n  | ( $prod / (($ssf * $ssg) | sqrt ) );\r\n```\r\n\r\nAnd here is a simple object-oriented (!) example:\r\n```\r\n[ {\"a\": {\"b\": 1}}, {\"a\": {\"b\": 2}} ] | summation( .a.b ) # => 3\r\n```"}}