Skip to content

Commit

Permalink
Add filter command as an alias for map(select())
Browse files Browse the repository at this point in the history
I (and I've heard others) use this often enough that it makes sense to have more user-friendly syntax.
  • Loading branch information
Benjamin E. Seidenberg committed Sep 20, 2024
1 parent 860af44 commit 3c9671f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/content/manual/dev/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,21 @@ sections:
output: ['{"id": "second", "val": 2}']


- title: "`filter(boolean_expression)`"
body: |
The function `filter(f)` takes an array and returns an array
with only the elements for which `f` returns true for that element.
The behavior is identical to map(select(`f`)) but it's more user
friendly.
examples:
- program: 'filter(. >= 2)'
input: '[1,5,3,0,7]'
output: ['[5,3,7]']


- title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `normals`, `finites`, `strings`, `nulls`, `values`, `scalars`"
body: |
Expand Down
1 change: 1 addition & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ def halt_error: halt_error(5);
def error(msg): msg|error;
def map(f): [.[] | f];
def select(f): if f then . else empty end;
def filter(f): map(select(f));
def sort_by(f): _sort_by_impl(map([f]));
def group_by(f): _group_by_impl(map([f]));
def unique: group_by(.) | map(.[0]);
Expand Down

0 comments on commit 3c9671f

Please sign in to comment.