Skip to content

Commit 919d396

Browse files
author
Benjamin E. Seidenberg
committed
Add filter command as an alias for map(select())
I (and I've heard others) use this often enough that it makes sense to have more user-friendly syntax.
1 parent 860af44 commit 919d396

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,21 @@ sections:
12071207
output: ['{"id": "second", "val": 2}']
12081208

12091209

1210+
- title: "`filter(boolean_expression)`"
1211+
body: |
1212+
1213+
The function `filter(f)` takes an array and returns an array
1214+
with only the elements for which `f` returns true for that element.
1215+
1216+
The behavior is identical to map(select(`f`)) but it's more user
1217+
friendly.
1218+
1219+
examples:
1220+
- program: 'filter(. >= 2)'
1221+
input: '[1,5,3,0,7]'
1222+
output: ['[5,3,7]']
1223+
1224+
12101225
- title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `normals`, `finites`, `strings`, `nulls`, `values`, `scalars`"
12111226
body: |
12121227

jq.1.prebuilt

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.jq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ def halt_error: halt_error(5);
22
def error(msg): msg|error;
33
def map(f): [.[] | f];
44
def select(f): if f then . else empty end;
5+
def filter(f): map(select(f));
56
def sort_by(f): _sort_by_impl(map([f]));
67
def group_by(f): _group_by_impl(map([f]));
78
def unique: group_by(.) | map(.[0]);

tests/man.test

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)