Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant subexpression in Generators and iterators example in doc #3222

Closed
jhcarl0814 opened this issue Dec 14, 2024 · 1 comment · Fixed by #3239
Closed

Redundant subexpression in Generators and iterators example in doc #3222

jhcarl0814 opened this issue Dec 14, 2024 · 1 comment · Fixed by #3239
Labels

Comments

@jhcarl0814
Copy link
Contributor

Describe the bug
jq 1.7 Manual / Generators and iterators

To Reproduce
In the first example:
jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'

  1. Redundant operation: else . produces an extra value beyond upto, then | select((by > 0 and . < upto) or (by < 0 and . > upto)) filters it.
  2. Should result in empty when init == upto.

Expected behavior
Can be simplified and corrected to:
jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'

Environment (please complete the following information):
/

Additional context
/

@itchyny itchyny added the docs label Jan 26, 2025
@itchyny
Copy link
Contributor

itchyny commented Jan 26, 2025

@jhcarl0814 Could you submit a PR? Thanks.

jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
jhcarl0814 added a commit to jhcarl0814/jq that referenced this issue Jan 26, 2025
jqlang#3222
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'`
Can be simplified and corrected to:
`jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants