Add a bit more text about generators
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nico Williams · parent ddef804 · Browse files · 2 files·+25 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+25 added−15 removed
- other:+25 added−15 removed
Provenance
- Commit
d8327a9- Tree
c22c82c- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
ddef804
2 files changed+25 added−15 removed
modified:docs/content/manual/manual.yml+14 added-10 removed
522 unmodified lines523 524 You can also use this on an object, and it will return all525 the values of the object.526 527+ Note that the iterator operator is a generator of values.528+529 examples:530 - program: '.[]'531 input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'532 output:29 unmodified lines562 outputs produced by the right. For instance, filter `.foo,563 .bar`, produces both the "foo" fields and "bar" fields as564 separate outputs.565 566+ The `,` operator is one way to contruct generators.567+568 examples:569 - program: '.foo, .bar'570 input: '{"foo": 42, "bar": "something else", "baz": true}'571 output: ['42', '"something else"']10 unmodified lines582 body: |583 584 The | operator combines two filters by feeding the output(s) of585 the one on the left into the input of the one on the right. It's582- pretty much the same as the Unix shell's pipe, if you're used to583- that.586+ similar to the Unix shell's pipe, if you're used to that.587 588 If the one on the left produces multiple results, the one on589 the right will be run for each of those results. So, the590 expression `.[] | .foo` retrieves the "foo" field of each588- element of the input array.591+ element of the input array. This is a cartesian product,592+ which can be surprising.593 594 Note that `.a.b.c` is the same as `.a | .b | .c`.595 596 Note too that `.` is the input value at the particular stage2524 unmodified lines3121 generates all the values in its input (which must be an3122 array or an object), `range(0; 10)` generates the integers3123 between 0 and 10, and so on.3124 3121- Even the comma operator is a generator, generating first the3122- values generated by the expression to the left of the comma,3123- then for each of those, the values generate by the3124- expression on the right of the comma.3125+ Even the comma operator is a generator, generating first3126+ the values generated by the expression to the left of the3127+ comma, then the values generated by the expression on the3128+ right of the comma.3129 3130 The `empty` builtin is the generator that produces zero3131 outputs. The `empty` builtin backtracks to the preceding3132 generator expression.3133 3134 All jq functions can be generators just by using builtin3131- generators. It is also possible to define new generators3132- using only recursion and the comma operator. If the3133- recursive call(s) is(are) "in tail position" then the3135+ generators. It is also possible to construct new generators3136+ using only recursion and the comma operator. If3137+ recursive calls are "in tail position" then the3138 generator will be efficient. In the example below the3139 recursive call by `_range` to itself is in tail position.3140 The example shows off three advanced topics: tail recursion,3141 generator construction, and sub-functions.544 unmodified linesmodified:jq.1.prebuilt+11 added-5 removed
1 .2-.TH "JQ" "1" "July 2023" "" ""2+.TH "JQ" "1" "August 2023" "" ""3 .4 .SH "NAME"5 \fBjq\fR \- Command\-line JSON processor6 .442 unmodified lines449 .450 .P451 You can also use this on an object, and it will return all the values of the object\.452 .453+.P454+Note that the iterator operator is a generator of values\.455+.456 .IP "" 4457 .458 .nf459 22 unmodified lines482 .483 .SS "Comma: ,"484 If two filters are separated by a comma, then the same input will be fed into both and the two filters\' output value streams will be concatenated in order: first, all of the outputs produced by the left expression, and then all of the outputs produced by the right\. For instance, filter \fB\.foo, \.bar\fR, produces both the "foo" fields and "bar" fields as separate outputs\.485 .486+.P487+The \fB,\fR operator is one way to contruct generators\.488+.489 .IP "" 4490 .491 .nf492 13 unmodified lines506 .507 .IP "" 0508 .509 .SS "Pipe: |"504-The | operator combines two filters by feeding the output(s) of the one on the left into the input of the one on the right\. It\'s pretty much the same as the Unix shell\'s pipe, if you\'re used to that\.510+The | operator combines two filters by feeding the output(s) of the one on the left into the input of the one on the right\. It\'s similar to the Unix shell\'s pipe, if you\'re used to that\.511 .512 .P507-If the one on the left produces multiple results, the one on the right will be run for each of those results\. So, the expression \fB\.[] | \.foo\fR retrieves the "foo" field of each element of the input array\.513+If the one on the left produces multiple results, the one on the right will be run for each of those results\. So, the expression \fB\.[] | \.foo\fR retrieves the "foo" field of each element of the input array\. This is a cartesian product, which can be surprising\.514 .515 .P516 Note that \fB\.a\.b\.c\fR is the same as \fB\.a | \.b | \.c\fR\.517 .3034 unmodified lines3552 .SS "Generators and iterators"3553 Some jq operators and functions are actually generators in that they can produce zero, one, or more values for each input, just as one might expect in other programming languages that have generators\. For example, \fB\.[]\fR generates all the values in its input (which must be an array or an object), \fBrange(0; 10)\fR generates the integers between 0 and 10, and so on\.3554 .3555 .P3550-Even the comma operator is a generator, generating first the values generated by the expression to the left of the comma, then for each of those, the values generate by the expression on the right of the comma\.3556+Even the comma operator is a generator, generating first the values generated by the expression to the left of the comma, then the values generated by the expression on the right of the comma\.3557 .3558 .P3559 The \fBempty\fR builtin is the generator that produces zero outputs\. The \fBempty\fR builtin backtracks to the preceding generator expression\.3560 .3561 .P3556-All jq functions can be generators just by using builtin generators\. It is also possible to define new generators using only recursion and the comma operator\. If the recursive call(s) is(are) "in tail position" then the generator will be efficient\. In the example below the recursive call by \fB_range\fR to itself is in tail position\. The example shows off three advanced topics: tail recursion, generator construction, and sub\-functions\.3562+All jq functions can be generators just by using builtin generators\. It is also possible to construct new generators using only recursion and the comma operator\. If recursive calls are "in tail position" then the generator will be efficient\. In the example below the recursive call by \fB_range\fR to itself is in tail position\. The example shows off three advanced topics: tail recursion, generator construction, and sub\-functions\.3563 .3564 .IP "" 43565 .3566 .nf554 unmodified lines