diff --git a/builtin.c b/builtin.c index 43cfe267..a3c1f78b 100644 --- a/builtin.c +++ b/builtin.c @@ -295,6 +295,7 @@ static const char* jq_builtins[] = { "def select(f): if f then . else empty end;", "def sort_by(f): _sort_by_impl(map([f]));", "def group_by(f): _group_by_impl(map([f]));", + "def unique: group_by(.) | map(.[0]);", }; diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 0e16a495..2e616e14 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -542,6 +542,18 @@ sections: input: '[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]' output: ['[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]'] + - title: `unique` + body: | + + The `unique` function takes as input an array and produces + an array of the same elements, in sorted order, with + duplicates removed. + + examples: + - program: 'unique' + input: '[1,2,5,3,5,3,1,3]' + output: ['[1,2,3,5]'] + - title: `contains` body: | diff --git a/testdata b/testdata index e08e6aa5..cc8263b0 100644 --- a/testdata +++ b/testdata @@ -448,3 +448,7 @@ sort [{"a": 4, "b": 1, "c": 3}, {"a": 0, "b": 2, "c": 43}, {"a": 1, "b": 4, "c": 3}, {"a": 1, "b": 4, "c": 14}] [[{"a": 4, "b": 1, "c": 3}], [{"a": 0, "b": 2, "c": 43}], [{"a": 1, "b": 4, "c": 14}, {"a": 1, "b": 4, "c": 3}]] [[{"a": 1, "b": 4, "c": 14}, {"a": 0, "b": 2, "c": 43}], [{"a": 4, "b": 1, "c": 3}, {"a": 1, "b": 4, "c": 3}]] + +unique +[1,2,5,3,5,3,1,3] +[1,2,3,5]