]> granicus.if.org Git - jq/commitdiff
Added unique_by function
authorSantiago Lapresta <santiago.lapresta@gmail.com>
Sun, 11 May 2014 23:37:27 +0000 (01:37 +0200)
committerSantiago Lapresta <santiago.lapresta@gmail.com>
Sun, 11 May 2014 23:37:27 +0000 (01:37 +0200)
builtin.c
docs/content/3.manual/manual.yml

index 480fb8ae6cbcc455d50bb690d6f8289f291dd637..c54a47c735ca08ce02126a1e839889881162a6bb 100644 (file)
--- a/builtin.c
+++ b/builtin.c
@@ -664,6 +664,7 @@ static const char* const jq_builtins[] = {
   "def sort_by(f): _sort_by_impl(map([f]));",
   "def group_by(f): _group_by_impl(map([f]));",
   "def unique: group_by(.) | map(.[0]);",
+  "def unique_by(f): group_by(f) | map(.[0]);",
   "def max_by(f): _max_by_impl(map([f]));",
   "def min_by(f): _min_by_impl(map([f]));",
 #include "libm.h"
index c8fcb5fa9ec707f203be8aee0d9d798bdb78a5a1..2e2fb36602709d9af40d58220e72377676619f88 100644 (file)
@@ -931,6 +931,21 @@ sections:
             input: '[1,2,5,3,5,3,1,3]'
             output: ['[1,2,3,5]']
 
+      - title: `unique_by`
+        body: |
+          
+          The `unique_by(.foo)` function takes as input an array and produces
+          an array of the same elements, in sorted order, with
+          elqements with a duplicate `.foo` field removed. Think of it as making
+          an array by taking one element out of every group produced by
+          `group_by`.
+        
+        examples:
+          - program: 'unique'
+            input: '[1,2,5,3,5,3,1,3]'
+            output: ['[1,2,3,5]']
+
+
       - title: `reverse`
         body: |